Codeforces Round #268 (Div. 2) A B C D

Codeforces Round #268 (Div. 2)

AI Wanna Be the Guy

水题

#include<bits/stdc++.h>
using namespace std;

int a[1000]={0};
int main(){
	int n,p,c;
	cin>>n;
	for(int i=0;i<2;i++){
		cin>>p;
		for(int j=0;j<p;j++){
			cin>>c;
			a[c]=1;
		}
	}
	for(int i=1;i<=n;i++){
		if(!a[i]) {
			cout<<"Oh, my keyboard!
";
			return 0;
		}
	}
	cout<<"I become the guy.
";
	return 0;
}

  BChat Online

枚举第二个人的起床时间

#include<bits/stdc++.h>
using namespace std;

struct node{
	int l,r;
};
node np[100],nq[100];
int p,q,l,r;
int main(){
	cin>>p>>q>>l>>r;
	for(int i=0;i<p;i++){
		cin>>np[i].l>>np[i].r;
	}
	for(int i=0;i<q;i++){
		cin>>nq[i].l>>nq[i].r;
	}
	int cnt=0;
	for(int i=l;i<=r;i++){
		for(int j=0;j<q;j++){
			nq[j].l+=i;nq[j].r+=i;
		}
		bool flag=false;
		for(int j=0;j<q;j++){
			int len1=nq[j].r-nq[j].l;
			for(int k=0;k<p;k++){
				int len2=np[k].r-np[k].l;
				if((len1>=nq[j].r-np[k].r&&nq[j].r-np[k].r>=0)||(len2>=np[k].r-nq[j].r&&np[k].r-nq[j].r>=0)){
					flag=true;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) cnt++;
		for(int j=0;j<q;j++){
			nq[j].l-=i;nq[j].r-=i;
		}
	}
	cout<<cnt<<"
";
	return 0;
}

  C:24 Game

题意:初始你手中有n张牌,数字分别是1~n,然后你任选其中两张牌,用“+”,“-”,“*”,来组合出另一张牌(另一个数字),并把使用过的牌扔掉,问最后能否剩下的最后一张牌为24?如果不能,输出NO;否则,输出YES,并将如何得到24的方法打印出来。

构造,当n<4时,不可能存在,

n==4时存在1*2*3*4=24,

当n==5时,存在5-1=4,4-2=1,现在剩下2*3*4=24,

n为6时,1*2*3*4=24,6-5=1,1*24=24,n>5时n为偶数时,n-(n-1)=1,将大于4的全部转换成1,

n>5时n为奇数时,可以转化成5

#include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin>>n;
	if(n<4) cout<<"NO
";
	else {
		cout<<"YES
";
		if(n%2==0){
			cout<<"1 * 2 = 2
";
			cout<<"2 * 3 = 6
";
			cout<<"6 * 4 = 24
";
			for(int i=5;i<=n;i+=2){
				cout<<i+1<<" - "<<i<<" = 1
";
				cout<<"1 * 24 = 24
";
			}	
		}
		else{
			cout<<"4 - 2 = 2
";
			cout<<"5 - 1 = 4
";
			cout<<"2 * 3 = 6
";
			cout<<"6 * 4 = 24
";
			for(int i=6;i<=n;i+=2){
				cout<<i+1<<" - "<<i<<" = 1
";
				cout<<"1 * 24 = 24
";
			}
		}
	}
	
	return 0;
}

  D:Two Sets

题意:将n个数分成两个集合。 如果x属于集合A,那么a - x一定属于集合A,如果x属于集合B,那么b - x一定属于集合B

首先很容易想到将,x与a - x同时存在,那么将它们放在A集合,同理B

但是这是错的,举个例子,a=3,b=6,   x: 1 2 4, x = 1 , a - x = 2 , 所以 1 、2  放入集合A ,x = 4 , b - 4 = 2  , 那么2 , 4在B集合 ,

如果a - x不存在,那么x一定不在A集合。

为什么?? 因为,在A中没有与之相对应的a - x。。。。...

同样。。如果b -  x不存在,那么x一定不在B集合里面。

如果a - x不存在,可以将 x 放在B集合,如果b -  x不存在将 x 放在A集合,

如果一个x既不属于A也不属于B,那么这个x将会和集合A合并也会和集合B合并,即 A==B

如果一个x既属于A也属于B,那么a - x将会和集合B合并 ,b - x 和集合B合并, 而a - x,b - x , x 是同一个集合,所以集合A==B 

所以A==B输出NO,

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 +100;
int n, a, b;
int f[N];
int p[N];
map<int, int> mp; 

int find(int x){
	if(f[x] == -1) return x;
	else return f[x] = find(f[x]);
}

void unity(int x,int y){
	x = find(x), y = find(y);
	if(x == y) return ;
	f[x] = y;
}
int main(){
	cin >> n >> a >> b;
	memset(f, -1, sizeof(f));
	for(int i = 1; i <= n; i++){
		cin >> p[i];
		mp[p[i]] = i;
	}
	
	for(int i = 1; i <= n; i++){
		if(mp[a - p[i]]){
			unity(i, mp[a-p[i]]);
		}
		else unity(i, n + 1);//B
		if(mp[b - p[i]]){
			unity(i, mp[b - p[i]]);
		}
		else unity(i, n + 2);//A
	}
	
	if(find(n + 1) == find(n + 2)){
		cout << "NO
";
	}
	else{
		cout << "YES
";
		for(int i = 1; i <= n; i++){
			if(find(i) == find(n + 2)){
				cout<<(i == n ? "0
" : "0 ");
			}
			else{
				cout<<(i == n ? "1
" : "1 ");
			}
		}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/YJing814/p/11170690.html