一些小tip

一些小tip

快读

读入速度快于scanf,远快于cin。

模板。

ch=getchar()写在while的条件里时耗时会增加。

int read(){
    int x=0,w=1;
	char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}

离散化

一种是map

	for(int i=1;i<=n;++i) s[i]=a[i]=read();
	sort(s+1,s+n+1);
	for(int i=1;i<=unique(s+1,s+n+1)-s-1;++i)
	lsh[s[i]]=i;
	for(int i=1;i<=n;++i) a[i]=lsh[s[i]];

一种是lower_bound()

for(int i=1;i<=n;++i) s[i]=a[i]=read();
	sort(s+1,s+n+1);	
	for(int i=1,tot=unique(s+1,s+n+1)-s-1;i<=n;++i)
	a[i]=lower_bound(s+1,s+tot+1,a[i])-s;

当测试数据为一组含有1005(1e3)个数的随机数据时,map耗时0.03s,lower_bound()耗时0.01s。

当测试数据为一组含有1005(1e4)个数的随机数据时,map耗时1.00s,lower_bound()耗时0.02s。

当测试数据为一组含有100005(1e5)个数的随机数据时,map无输出,lower_bound()耗时0.15s。

综上可知,离散化时lower_bound()效率更高。

deque的空间比vector大

posted @ 2020-10-23 20:34  林生。  阅读(75)  评论(0)    收藏  举报