索引:
类型取值范围,lower_bound,离散化,整除,快读,对拍,测时间,随机数,随机树
-
类型取值范围:
unsigned int 0~4294967295
int -2147483648~2147483647
unsigned long 0~4294967295
long -2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
-
STL二分
lower_bound(,,)-数组-1;//第一个大于或等于被查数的值
upper_bound(,,)-数组-1;//第一个大于被查数的值
-
离散化模板
for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+len+1,a[i])-b;
-
整除
a∣b:使b=ka,则称a整除b,或b能被a整除,记为a∣b。
-
快读快输
https://blog.csdn.net/weixin_44577381/article/details/86567132
int inline read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
-
对拍
#include<cstdio>
#include<cstdlib>
#include<ctime>
int main()
{ long s,t;
while(1){
system("cls");
do{
system("data > data.in"); //data是数据生成程序
s=clock();
system("mycode < data.in > mycode.out"); //mycode是要交的程序
t=clock();
system("correct < data.in > correct.out"); //correct是正确的程序
if(system("fc mycode.out correct.out > nul")) break;
else printf("AC time: %ldms
",t-s);
}while(1);
printf("WA time: %ldms
",t-s); //运行时间
system("fc mycode.out correct.out");
system("pause>nul");
}
return 0;
}
-
测时间
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
clock_t start,end;
int main()
{
start=clock();//开始计时
int ans=0;
for(int i=1;i<=1e8;i++) ans++;
end=clock();//结束用时
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
cout<<"Total time:"<<endtime<<endl;//s为单位
cout<<"total time:"<<endtime*1000<<"ms"<<endl;//ms为单位
system("pause");
return 0;
}
-
随机数
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)(time(NULL)));
for(int i=0;i<10;i++)
{
cout<<rand()%2<<endl;
}
return 0;
}
- 随机树
#include <bits/stdc++.h>
using namespace std;
int fa[100005],f[100005];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
srand(time(0));
int n=10;//节点个数,自行更改
for(int i=1;i<=n;i++)fa[i]=i;
int root=rand()%n+1;
cout<<n<<endl;
for(int i=1;i<=n;i++)
{
if(i==root) continue;
int father;
do{
father=rand()%n+1;
}while(find(i)==find(father));
f[i]=father;
fa[find(i)]=find(father);
}
for(int i=1;i<=n;i++)
{
if(root==i) continue;
cout<<i<<' '<<f[i]<<' '<<rand()%100+1<<endl;//相连的两个节点及边权
}
}