Codeforces Round #535 (Div. 3) A-E1题解

比赛链接

A. Two distinct points

两条线段的四个端点中一定有两个来自不同线段的端点坐标不同,找出任意两个满足条件的即可。

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();num=0;T f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
int co[10010];

int main(){
	int q;read(q);
	rep(i,1,q){
		int l1,r1,l2,r2;read(l1);read(r1);read(l2);read(r2);
		if(l1==r2){
			cout<<r1<<" "<<r2<<endl;continue;
		}
		cout<<l1<<" "<<r2<<endl;
	}
	return 0;
}

B. Divisors of Two Integers

由于一个数就是本身的一个约数,所以该数列中最大的数一定是两个答案之一,那么在与原数列中剔除掉该数的所有约数(两个数的公约数需特判),则剩下的最大的数就是第二个答案。

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();num=0;T f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
int co[150];int temp[150];

int main(){
	int n;read(n);
	rep(i,1,n){read(co[i]);}
	sort(co+1,co+n+1);
	int ans1=0;int ans2=0;
	ans1=co[n];
	
	int len=0;
	rep(i,1,n){
		if(co[i]==co[i-1]){temp[++len]=co[i];continue;}
		if(ans1%co[i]!=0){temp[++len]=co[i];}
	}	
	
	ans2=temp[len];
	cout<<ans1<<" "<<ans2<<endl;
	return 0;
}

C. Nice Garland

我们按下标模3的余数将位置分成三种,则每一种颜色只可能存在于最多一种位置上,除去总个数<3的情况,可得结论:每种位置上有且只有一种颜色,那么我们枚举6种颜色与位置的对应关系,统计出费用最小的即可。

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();num=0;T f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
char co[200010];char temp[200010];char ans[200010];
int num[4];char v[4];

int main(){
	int n;read(n);
	rep(i,1,n){
		cin>>co[i];
	}
	v[1]='R';v[2]='G';v[3]='B';
	rep(i,1,3){num[i]=i;}
	
	int fans=n;
	do{
		int ret=0;
		rep(i,1,n){
			temp[i]=co[i];
			if(co[i]!=v[num[i%3+1]]){
				ret++;
				temp[i]=v[num[i%3+1]];
			}
		} 
		if(ret<fans){
			fans=ret;
			rep(i,1,n){
				ans[i]=temp[i];
			}
		}
	}while(next_permutation(num+1,num+4));
	
	write(fans);
	rep(i,1,n){cout<<ans[i];}
	cout<<endl;
	return 0;
} 

D. Diverse Garland

(明明比C简单,干嘛放在D题上吓人。。。。)

由于一个位置最多与两个位置相邻,所以对于每一个不满足条件的位置我们总能找到一种颜色使其符合条件。且可保证这种方案费用最小。

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();num=0;T f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
char co[200010];

int main(){
	int n;read(n);
	rep(i,1,n){cin>>co[i];} 
	
	int ans=0;
	rep(i,2,n){
		if(co[i]==co[i-1]){
			ans++;
			if('R'!=co[i-1]&&'R'!=co[i+1])co[i]='R';
			if('G'!=co[i-1]&&'G'!=co[i+1])co[i]='G';
			if('B'!=co[i-1]&&'B'!=co[i+1])co[i]='B';
		}
	}
	write(ans);
	rep(i,1,n){
		cout<<co[i];
	}
	cout<<endl;
	return 0;
}

E1. Array and Segments (Easy version)

我们可以枚举两个最小值与最大值的位置,然后贪心选取m个区间,除了只减掉最大值所在位置而没有减到最小值所在位置的区间不取之外,其他区间均对不会对答案产生负面贡献。

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();num=0;T f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
int co[310];int cz[310][2];
int ans[310];int temp[310];

int main(){
	int n,m;read(n);read(m);
	rep(i,1,n){read(co[i]);}
	rep(i,1,m){read(cz[i][0]);read(cz[i][1]);} 
	
	ans[301]=0;
	rep(i,1,n){
		rep(j,1,n){
			if(i==j)continue;
			int nop1=co[i];int nop2=co[j];temp[0]=0;
			rep(k,1,m){
				if((cz[k][0]>i||cz[k][1]<i)&&cz[k][0]<=j&&cz[k][1]>=j){nop2--;temp[++temp[0]]=k;}
			}
			if(nop1-nop2>ans[301]){
				ans[0]=temp[0];ans[301]=nop1-nop2;
				rep(k,1,temp[0]){ans[k]=temp[k];}
			}
		}
	}
	write(ans[301]);write(ans[0]);
	rep(i,1,ans[0]){
		qwq(ans[i]);putchar((i==ans[0])?'\n':' ');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Bill_Benation/article/details/86776515
今日推荐