板子大集

· · 个人记录

快读

template <typename T>
inline T read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x*10+ch-'0';
        ch=getchar(); 
    }
    return x*f;
}

对拍

代码文件:st.cpp;暴力代码文件:bf.cpp;数据文件:data.cpp;对拍文件:对拍.cpp.

生成随机数

#include <bits/stdc++.h>
using namespace std;
const int MOD=1e4;
const int MODD=1e9;
int random(int n){
    return (int)rand()%n;
}
int main(){
    freopen("sample.in","w",stdout);
    srand((unsigned)time(0));
    int n=rand()%MOD;
    int inf=MODD;
    cout<<n<<"\n";
    for(int i=1;i<=n;i++){
        int op=rand()%2;
        if(op)cout<<random(inf)<<" ";
        else cout<<random(inf)*(-1)<<" ";
    }
    return 0;
} 

对拍文件

#include <bits/stdc++.h>
using namespace std;
int main() {
    int data=0;
    while(1) {
        ++data;
        system("C:\\gen.exe");
        double st=clock();
        system("C:\\std.exe");
        double ed=clock();
        system("C:\\bf.exe");
        if(system("fc C:\\sample.out C:\\ans.out")) {
            puts("王安遂");
            break;
        } else if(ed-st>=1000) {
            puts("特了鄂");
            break;
        } else printf("Accepted,test#%d,cost time %.0lfms\n",data,ed-st);
    }
}

生成树:

对任意点i,在[1,i-1]中随机一个父亲

生成图:

无要求:随机m条边的两端

要求无自环:两端相等时continue

要求联通:在树的基础上加边

要求无重边:map存所有边

STL

STL的地址特点:左闭右开

查重

unique(a+0,a+n):将有序数组中的重复元素挑出(返回指向结束位置的指针)

无重复部分的长度:unique(a,a+n)-a;

二分

lower_bound(a,a+n,num)查找第一个大于等于num的位置,如果不存在则返回右端点。

upper_bound(a,a+n,num)查找第一个大于num的位置,如果不存在则返回右端点。

得到的所有下标都是返回值减数组名

全排列

next_permtation(a,a+n); 求下一种排列方式;

返回值:如果当前为(字典序)最大的排列方式,返回0,否则返回1.

prev_permtation(a,a+n); 求前一种排列方式。

赋值

memset(数组名,值,sizeof(数组名));

pair

pair<类型,类型> p;

可嵌套,自带比较函数,成员是p.first和p.second;

makepair(值,值);制造pair。

find(x)

寻找区间中第一个等于x的位置,不要求有序,返回地址。

stack和queue(不建议用,建议手写,否则很慢)

priority_queue

对于结构体,需要重载运算符。大顶堆重载<,小顶堆重载>。(记得在括号后面加const)

deque

可以在队头和队尾分别进行出入队。静态空间占用大,谨慎使用。

入:push_front(或back)(x);

出:pop_front(或back)();

vector

可用于邻接表存图。

排序时:sort(a.begin(),a.end());时刻牢记左闭右开

map

在遍历时除了直接定义迭代器之外还可以写成

for(auto &it : mp)

string的排序

sort(s.begin(),s.end());

set和multiset

有序去重/有序不去重

multiset做erase会把同类元素全删掉。只删一个:s.erase(s.find(元素))【必须保证元素存在】