前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Educational Codeforces Round 65 (Rated for Div. 2) A-D

Educational Codeforces Round 65 (Rated for Div. 2) A-D

作者头像
用户2965768
发布2019-06-14 20:50:54
3290
发布2019-06-14 20:50:54
举报
文章被收录于专栏:wymwymwym

A.水题

//A
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int t,n;
	string s;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		cin>>s;
		int st = (int)s.find('8');
		int sz = s.size() - st;
		//printf("%d %d\n",st,sz);
		if(st!=-1&&sz>=11){
			cout<<"yes\n";
		}else cout<<"no\n";
	}
	return 0;
}

B.考察全排列

//B
#include <bits/stdc++.h>
using namespace std;
int a[10] = {4,8,15,16,23,42};
int d[5];
int main()
{
	for(int i=1;i<=4;i++){
		printf("? %d %d\n",i,i+1);
		fflush(stdout);
		cin>>d[i];
	}
	do{
		if(a[0]*a[1]==d[1]&&a[1]*a[2]==d[2]&&a[2]*a[3]==d[3]&&a[3]*a[4]==d[4]){
			break;
		}
		
	}while(next_permutation(a,a+6));
	printf("!");
	for(int i=0;i<6;i++){
		printf(" %d",a[i]);
	}
	printf("\n"); 
	fflush(stdout);
	return 0;
}

C.并查集

rank是能够通知到的人的个数,比较当前这两个人谁能通知到的人较少,然后把能通知到的人较少的那个人的指向pre[a]指向b(假设a能通知到的人较少),这样是为了减少最后那个for循环的操作,因为你之前把一些不联通的几个子图连到了一起,然而每一个子图里的非根节点的pre还未改变,你需要最后再把它们的指向改一遍。你每次联通的时候都是把人较少的联通到人较多的,最后for循环里改变的次数较少。不过你不比较的话也一样,最后都会指向一个人。

//C
#include <bits/stdc++.h>
using namespace std;
const int N = 600005;
int n,m;
int f[N];
int rank1[N];
int find(int x) {
	int g = x;
	while(x!=f[x]) {
		x = f[x];
		rank1[g]++;
	}
	return x;
}
int find1(int x) {
	while(x!=f[x]) {
		x = f[x];
	}
	return x;
}

int main() {
	scanf("%d %d",&n,&m);
	for(int i=0; i<=n+1; i++)f[i] = i,rank1[i] = 1;
	for(int i=1; i<=m; i++) {
		int k,t,t2;
		scanf("%d",&k);
		if(k==0)continue;
		scanf("%d",&t);
		for(int j=0; j<k-1; j++) {
			scanf("%d",&t2);
			int a = find(t);
			int b = find(t2);
			if(a!=b) {
				if(rank1[a]>rank1[b]) {
					f[b] = a;
					rank1[a]+=rank1[b];
				} else {
					f[a] = b;
					rank1[b]+=rank1[a];
				}
			}

		}
	}
	for(int i=1; i<=n; i++) {
		f[i] = find1(f[i]);
		rank1[i] = rank1[find(f[i])];
	}
	for(int i=1; i<=n; i++) {
		if(i==1)printf("%d",rank1[i]);
		else printf(" %d",rank1[i]);
		
	}
	return 0;

}

D.贪心+栈

求最小的最大值,平均分配即可

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	stack<char> str1,str2;
	string s;
	char ans[200005];
	cin>>s;
	for(int i=0;i<n;i++){
		if(s[i]=='('){
			if(str1.size()<str2.size()){
				str1.push(s[i]); 
				ans[i] = '0';
			}else
			{
				str2.push(s[i]);
				ans[i] = '1';
			}
		}else{
			if(str1.size()>str2.size()){
				str1.pop();
				ans[i] = '0';
			}else{
				str2.pop();
				ans[i] = '1';
			}
		} 		
	}
	ans[n]='\0';
	cout<<ans<<endl;
	return 0;
 } 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档