AtCoder Beginner Contest 174 总结

翘了实时比赛,今天用virtual补一下......发现题变水了......怕是错失了一次上分机会。

A,B,C略。D题每次交换尽可能远的一对"W......R"。E题二分答案,注意下边界初始化不能直接设成0,否则二分判断时除数太小精度爆炸。F虽然n还不小,但还是可以用莫队水过去(理论上O(n^{1.5})大于3e9,2000ms有点危险),正解应该是主席树或者离线+树状数组。主席树空间开大点没毛病......

A

#include
#include
#include
#include
using namespace std;
typedef long long ll;

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

int main() {
	int x=read();
	puts(x<30?"No":"Yes"); 
	return 0;
}

B

#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int n;
int ans;
double x,y,d;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

int main() {
	n=read();
	scanf("%lf",&d);
	d*=d;
	while (n--) {
		scanf("%lf%lf",&x,&y);
		ans+=(x*x+y*y<=d);
	}
	cout<

C

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=1e6+4;
int m,vis[N],x;
int tim;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

int main() {
	m=read();
	while (1) {
		++tim;
		x=(x*10+7)%m;
		if (!x) {
			printf("%d\n",tim);
			break;
		}
		if (vis[x]) {
			puts("-1");
			break;
		}
		vis[x]=true;
	}
	return 0;
}

D

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=2e5+4;
int n;
char s[N];
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
int ans;
int main() {
	n=read();
	scanf("%s",s+1);
	int l=1,r=n;
	while (l1) --r;
		if (l

E

#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=2e5+4;
int n;
ll m;
double a[N];
double l=1e9,r=0;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
template  inline void smax(T &x,T y) {
	x=x inline void smin(T &x,T y) {
	x=x>y?y:x;
}
inline bool ok(double x) {
	ll cnt=0;
	for (register int i=1;i<=n;++i) {
		cnt+=(ll)ceil(a[i]/x)-1;
		if (cnt>m) return false;
	}
	return true;
}
int main() {
	n=read(),m=read();
	for (register int i=1;i<=n;++i) {
		scanf("%lf",&a[i]);
		smax(r,a[i]);
		smin(l,a[i]/(m+1));
	}
	while (r-l>1e-2) {
		double mid=(l+r)/2;
		if (ok(mid)) r=mid;
		else l=mid;
	}
	cout<<(ll)ceil(l)<

F

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=5e5+4;
int n,m;
int root[N],sum[N*50],lc[N*50],rc[N*50];
int tim;
int nxt[N];
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
inline void pushup(int rt) {
	sum[rt]=sum[lc[rt]]+sum[rc[rt]];
}
inline void build(int &rt,int l,int r) {
	rt=++tim;
	if (l==r) return ;
	int mid=l+r>>1;
	build(lc[rt],l,mid);
	build(rc[rt],mid+1,r);
}
inline void modify(int &rt,int pre,int l,int r,int pos,int val) {
	rt=++tim;
	lc[rt]=lc[pre];
	rc[rt]=rc[pre];
	sum[rt]=sum[pre]+val;
	if (l==r) return ;
	int mid=l+r>>1;
	if (pos<=mid) modify(lc[rt],lc[pre],l,mid,pos,val);
	else modify(rc[rt],rc[pre],mid+1,r,pos,val);
	pushup(rt);
}
inline int query(int rt,int l,int r,int L) {
	if (l==r) return sum[rt];
	int mid=l+r>>1;
	if (mid>=L) return sum[rc[rt]]+query(lc[rt],l,mid,L);
	else return query(rc[rt],mid+1,r,L);
}
int main() {
//	freopen("f3.txt","r",stdin);
	n=read(),m=read();
	build(root[0],1,n);
	for (register int i=1;i<=n;++i) {
		int x=read();
		if (!nxt[x]) {
			modify(root[i],root[i-1],1,n,i,1);
		} else {
			int temp;
			modify(temp,root[i-1],1,n,nxt[x],-1);
			modify(root[i],temp,1,n,i,1);
		}
		nxt[x]=i;
	}
	while (m--) {
		int x=read(),y=read();
		printf("%d\n",query(root[y],1,n,x));
	}
	return 0;
}

 

你可能感兴趣的:(AtCoder总结,主席树)