Codeforces Round #664 (Div. 2)题解

本文详细解析了Codeforces Round #664 (Div. 2)比赛中的四道题目:A题考察回文串判断,B题涉及棋盘行走路径,C题结合位运算和动态规划,D题是关于序列操作的策略问题。每道题都提供了题意、思路和代码实现。

A Boboniu Likes to Color Balls

在这里插入图片描述
题意思路 : 给定四种颜色通过以下操作判断能否组成回文串
操作: 前三个减一 后一个加一
思路 : 判断奇数的个数 若和为奇数 则最多有一个奇数 若为偶数 0个
代码:


#include<bits/stdc++.h>
using namespace std;
int c[4];
bool f(){
	int sum1 = 0;
	int sum2 = 0;
	for(int i = 0; i < 4; i++){
		sum1 += c[i];
		if(c[i] % 2 != 0)
			sum2++;
	}
	if(sum1 % 2 == 0){
		if(sum2 != 0){
			//cout <<2;
			return false;
		}
	}
	else{
		if(sum2 != 1){
			//cout <<1;
			return false;
		}
	}
	return true;
}
int main(){
	int T;
	cin >> T;	
	while(T--){
		int r, g, b, w;
		for(int i = 0; i < 4; i++){
			cin >> c[i];
		}
		int flag = 0;
		if(!f()){
			for(int i = 0; i < 3; i++){
				if(c[i] > 0) c[i]-=1;
				else flag = 1;
			}
			c[3] += 3;
			
			if(!f()) flag = 1;
		}
		if(flag == 1) cout << "NO" << endl;
		else cout << "YES" << endl;	
	}
} 

B. Boboniu Plays Chess

在这里插入图片描述
题意 : 一个车从x, y走遍全图 给出路径
思路:当前行从左到右 然后向上到顶后向下 标记好方向
代码:

#include<bits/stdc++.h>
using namespace std;
int n, m, x, y;
int main(){
	cin >> n >> m >> x >> y;
	for(int i = x; i >= 1; i--){
		if(i == x){
			for(int j = y; j <= m; j++){
				cout << i << ' ' << j << endl;
				//cout << "+++++++++++++++++" << endl;
			}
			for(int j = y-1; j >= 1; j--){
				cout << i << ' ' << j << endl;
			}
		}
		else{
			if((x-i) % 2 != 0){
				for(int j = 1; j <= m; j++){
					cout << i << ' ' << j << endl;
				}
			}
			else{
				for(int j = m; j >= 1; j--){
					cout << i << ' ' << j << endl;
				}
			}
		}
		//cout << "+++++++++++++++++" << endl;
	}
	int flag;
	if((x - 1) % 2 != 0) flag = 1;
	else flag = 0;
	//cout <<flag << endl;
	for(int i = x+1; i <= n; i++){
		if((i-x) % 2 != 0){
			if(flag == 0){
				for(int j = 1; j <= m; j++){
					cout << i << ' ' << j << endl;
				}
			}
			else{
				for(int j = m; j >= 1; j--){
					cout << i << ' ' << j << endl;
				}
			}
		}
		else{
			if(flag == 0){
				for(int j = m; j >= 1; j--){
					cout << i << ' ' << j << endl;
				}
			}
			else{
				for(int j = 1; j <= m; j++){
					cout << i << ' ' << j << endl;
				}
			}
		}
		//cout << "+++++++++++++++++" << endl;
	}
	
	
}

C. Boboniu and Bit Operations在这里插入图片描述

位运算+dp

#include<bits/stdc++.h>
 
using namespace std;
 
const int N = 210,M=1<<10;
 
int a[N],b[N],dp[N][M];
int main()
{
	int n,m;
	cin >>n>>m;
	for(int i=1;i<=n;i++)	cin >>a[i];
	for(int i=1;i<=m;i++)	cin >>b[i];
	dp[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			int andd=a[i]&b[j];
			for(int k=0;k<M;k++)
			if (dp[i-1][k]) 
			{
				dp[i][k|andd]=1;
			}	
		}
	}
	for(int i=0;i<M;i++)
	{
		if(dp[n][i])
		{	
			cout <<i;
			break ;
		}
	}
	
	
}

D. Boboniu Chats with Du

在这里插入图片描述

给出一个序列,拿大于 m 的接下来 d天就不能拿了,拿小于 m 的还可以继续操作,求可以拿的最大值。
思路 : 先把大于m和小于等于m的区分开存放;假设当前有x个大于m的数,那么我们可以选择1~x个大于m的数,既然选了,肯定要选择最大的;假设我们选了y个,那么其中一个可以放在第n天,消耗1天;剩下的y-1个消耗 (y-1)(d+1) ; 现在总共消耗 (y-1)(d+1) +1天;剩下cnt = n-(y-1)*(d+1) +1天;这cnt天可以在小于m的数里面找,贪心找最大的前cnt个即可;
代码 :

#include<bits/stdc++.h>
using namespace std;
const int N =2e5+10;
typedef long long ll;
ll a[N],b[N];
bool cmp(int x,int y){
	return x>y;
}
int main(){
	int n,d,m;
	cin >> n>>d>>m;
	int x=0,y=0;
	for(int i=1;i<=n;i++){
		ll xx;
		cin >> xx;
		if(xx > m)
			a[++x]=xx;//闭嘴v
		else 
			b[++y]=xx;
	}
	sort(a+1, a+1+x, cmp);
	sort(b+1, b+1+y, cmp);
	for(int i = 1; i <= y; i++) 
		b[i]+=b[i-1];
	ll res = 0,ans = b[y];
	for(int i = 1; i <= x; i++){
		res += a[i];
		int pos = (i-1)*(d+1)+1;
		if(pos > n) break;
		pos = n-pos;
		pos = min(pos,y);
		ans = max(ans,res+b[pos]);
	}
	cout << ans << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值