绍一集训Round#2

Preface

感觉这次的题目是真的太水了,可能是为了让我们涨一波信心的吧。

不过最后一题没有想到那种玄学做法还是太菜了,还是要一波姿势的啊。


交换

一道入门难度题,根据排序不等式(又或是简单推导可以发现直接把一个数换到它该去的地方是肯定不会更差的。

因此我们直接模拟交换的过程即可,复杂度(O(n))

CODE

#include<cstdio>
#include<cctype>
using namespace std;
const int N=1000005;
int n,a[N],id[N],ans;
inline char tc(void)
{
	static char fl[100000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
	x=0; char ch; while (!isdigit(ch=tc()));
	while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
}
inline void swap(int &a,int &b)
{
	int t=a; a=b; b=t;
}
int main()
{
	freopen("swap.in","r",stdin); freopen("swap.out","w",stdout);
	register int i; read(n);
	for (i=1;i<=n;++i)
	read(a[i]),id[a[i]]=i;
	for (i=1;i<=n;++i)
	if (a[i]^i) id[a[i]]=id[i],swap(a[i],a[id[i]]),id[i]=i,++ans;
	return printf("%d",ans),0;
}

兔子抓狼

首先分析对于狼来说什么情况是最优的

如果一只兔子可以炸到它,那么它迟早会被这只兔子给炸了,否则兔子会一直保持在它的右边。

因此,我们分析出,狼直接走回家是对于它来说最好的选择

然后就很简单了,我们再来考虑兔子,由于如果一只兔子可以在狼回家的路上炸到狼,我们为了简化问题假定让兔子全部跑到狼的家门口去炸它。

这样我们对于所有兔子到达狼的家的距离开一个维护一下,然后每次取出堆顶判断是否可以炸到狼即可

CODE

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
using namespace std;
typedef double DB;
const int N=500005,tar=10000000;
int n,ans,x,y,t,tot;
priority_queue < DB,vector<DB>,greater<DB> > small;
inline char tc(void)
{
	static char fl[100000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
	x=0; char ch; int flag=1; while (!isdigit(ch=tc())) flag=ch^'-'?1:-1;
	while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc())); x*=flag;
}
int main()
{
	freopen("rabbit.in","r",stdin); freopen("rabbit.out","w",stdout);
	register int i; read(n); read(t);
	for (i=1;i<=n;++i)
	read(x),read(y),small.push((DB)sqrt(1LL*(x-tar)*(x-tar)+1LL*y*y));
	while (!small.empty())
	if (small.top()<=(DB)tar+tot) tot+=t,++ans,small.pop(); else break;
	return printf("%d",ans),0;
}

魔法

首先你要知道一个东西:01Trie

这个很简单,想当于建出一棵树,每一个节点都是一个0/1值。从根出发的某条路径上的点代表的就是一个二进制下的数

然后我们发现这个东西就可以很好的处理xor了

我们先把所有的点插入0/1Trie中,每一次查询的时候相当与询问大于等于一个数的两数xor值。

我们同时跳两条链,每次判断当前节点的值是否小于所求的点,若是则这两点必然异位

统计的时候也有个小Trick,分成一段一段来搞又快又方便

CODE

#include<cstdio>
#include<cctype>
using namespace std;
typedef long long LL;
const int N=50005,P=60;
struct Trie
{
	int ch[2],size;
}node[N*P];
int n,tot=1; LL x,ans,s=1;
inline char tc(void)
{
	static char fl[100000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
	x=0; char ch; while (!isdigit(ch=getchar()));
	while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=getchar()));
}
inline void read(LL &x)
{
	x=0; char ch; while (!isdigit(ch=getchar()));
	while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=getchar()));
}
inline void insert(LL x,int &now,int d)
{
	if (!now) now=++tot; ++node[now].size;
	if (!(~d)) return;
	if (x&(1LL<<d)) insert(x,node[now].ch[1],d-1); else insert(x,node[now].ch[0],d-1);
}
inline void query(int x,int y,LL s,int d)
{
	if (!x||!y) return; if ((1LL<<d+1)<=s) return;
	if ((1LL<<d)>=s)
	{
		ans+=1LL*node[node[x].ch[0]].size*node[node[y].ch[1]].size+1LL*node[node[x].ch[1]].size*node[node[y].ch[0]].size;
		query(node[x].ch[0],node[y].ch[0],s,d-1); query(node[x].ch[1],node[y].ch[1],s,d-1);
	} else query(node[x].ch[0],node[y].ch[1],s-(1LL<<d),d-1),query(node[x].ch[1],node[y].ch[0],s-(1LL<<d),d-1);
}
int main()
{
	freopen("magic.in","r",stdin); freopen("magic.out","w",stdout);
	register int i; read(n);
	for (i=1;i<=n;++i)
	{
		read(x); int now=1; insert(x,now,P-1);
	}
	for (i=1;i<=19;++i,s*=10) query(1,1,s,P-1);
	return printf("%lld",ans>>1),0;
}
原文地址:https://www.cnblogs.com/cjjsb/p/9446145.html