Codeforces Round 937(Div.4)

文章介绍了在Codeforces平台上的一系列C++编程题目,涉及条件判断、字符串操作、时间转换、二进制判断、因子检查和状态压缩等技术,展示了如何用C++解决这些问题。
摘要由CSDN通过智能技术生成

Problem - A - Codeforces

#include<bits/stdc++.h>
using namespace std;
 
void solve(){
	int a,b,c;
	cin >> a>> b >> c;
	if(a < b && b < c) cout <<"STAIR" << endl;
	else if(a < b && c < b) cout << "PEAK" << endl;
	else cout << "NONE" << endl;
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

Problem - B - Codeforces

#include<bits/stdc++.h>
using namespace std;
void solve(){
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++){
		string s = "";
		for(int j= 1;j <= n;j++){
			if(i % 2 == 1){
				if(j%2 == 1) s+="##";
				else s+="..";
			} else {
				if(j%2 == 1) s+="..";
				else s+="##";
			}
		}
		cout << s <<endl;
		cout << s << endl;
	}
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

Problem - C - Codeforces

#include<bits/stdc++.h>
using namespace std;
 
void solve(){
	string s;
	cin >> s;
	string ss="";
	int hour = (s[0]-'0')*10+s[1]-'0';
	if(hour >= 12) ss = "PM";
	else ss = "AM";
	if(hour==12) cout <<hour << ':' << s[3] <<s[4] <<" " << ss<<endl;
	else if(hour > 12) {
		if(hour%12<10)
			cout <<0<<hour%12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
		else cout <<hour%12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
	}
	else if(hour == 0) cout <<12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
	else cout << s <<" " <<ss<<endl;
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

Problem - D - Codeforces

枚举2000以内的二进制数据,依次判断即可

#include<bits/stdc++.h>
using namespace std;
int check(int x){
	while(x){
		int t = x%10;
		x/=10;
		if(t!=1 && t!= 0) return 0;
	}
	return 1;
}
int a[] = {10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111};
void solve(){
	int n;
	cin >> n;
	for(int i = 0;i < 14;i++){
		if(check(n)){
			puts("yes");
			return;
		}
		while(n%a[i] == 0){
			n/=a[i];
		}
	}
	if(check(n)) puts("yes");
	else puts("no");
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

 Problem - E - Codeforces

先预处理n的所有因子,在暴力判断每一个因子是否符合答案即可

#include<bits/stdc++.h>
using namespace std;
 
void solve(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector<int> q;
	for(int i = 1;i <= n;i++){
		if(n%i == 0) q.push_back(i);
	}
	for(int i = 0;i < q.size();i++){
		if(q[i] == n){
			cout << q[i] <<endl;
			return;
		}
		int cnt = 0;
		if(n / q[i] == 2){
			for(int j = 0,k = n/2;j<n/2&&k<n;j++,k++){
				if(s[j] != s[k]) cnt++;
				if(cnt > 2){
					break;
				}
			}
			if(cnt<=1){
				cout << q[i] <<endl;
				return;
			}
			else continue;
		}
		string ss;
		string a = s.substr(0,q[i]);
		string b = s.substr(q[i],q[i]);
		string c = s.substr(2*q[i],q[i]);
		if(a!=b && b!=c && a != c) continue;
		if(a==b || a==c) ss = a;
		else ss = b;
		int k = 0;
		for(int j = 0;j < n;j++){
			if(ss[k++] != s[j]){
				cnt++;
				if(cnt > 1){
					break;
				}
			}
			if(k%q[i]==0) k = 0;
		}
		if(cnt <= 1){
			cout << q[i] <<endl;
			return;
		}
	}
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

Problem - F - Codeforces

当一个点扩展为两个子节点时,叶子结点会增加1,因此c=a+1,不满足则输出-1

再贪心扩展树,为了使深度最小,一定要使得树接近二叉树,因此优先扩展需要两个儿子的结点,最后再扩展只有一个儿子的结点

#include<bits/stdc++.h>
using namespace std;
 
 
void solve(){
	int a,b,c;
	cin >> a>> b >> c;
	if(c != a+1){
		cout << -1 <<endl;
		return;
	}
	priority_queue<int,vector<int>,greater<int> > q;
	q.push(0);
	for(int i = 0;i < a;i++){
		int t = q.top();
		q.pop();
		q.push(t+1);
		q.push(t+1);
	}
		for(int i = 0;i < b;i++){
		int t = q.top();
		q.pop();
		q.push(t+1);
	}
	int ans = 0;
	while(!q.empty()){
		ans = max(ans,q.top());
		q.pop();
	}
	cout << ans <<endl;
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

Problem - G - Codeforces

状态压缩dp,将可以相邻连接的歌之间连一条双向边,dp[i][j]表示当前状态为i,且该歌链的最后一个为j,进行状态转移,当j与k有边且k在状态i中没有被选,则可以转移到dp[i|(1<<k)][k].

#include<bits/stdc++.h>
using namespace std;
 
string g[20],w[20];
int e[20][20];
int dp[(1<<17)+10][20];
void solve(){
	memset(e,0,sizeof(e));
	memset(dp,0,sizeof(dp));
	int n;
	cin >> n;
	for(int i = 0;i < n;i++) cin >> g[i] >> w[i];
	for(int i = 0;i < n;i++){
		for(int j = i+1;j <n;j++){
			if(g[i] == g[j] || w[i] == w[j]) e[i][j] = e[j][i] = 1;
		}
	} 
	for(int i = 0;i < n;i++){
		dp[(1<<i)][i] = 1;
	}
	for(int i = 0;i < (1<<n);i++){
		for(int j = 0;j < n;j++){
			if(dp[i][j]){
				for(int k = 0;k < n;k++){
					if((i>>k)&1) continue;
					if(e[j][k]) dp[(1<<k)|i][k] = 1;
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0;i < (1<<n);i++){
		for(int j = 0;j < n;j++){
			if(dp[i][j]) ans = max(ans,__builtin_popcount(i));
		}
	}
	cout << n - ans << endl;
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		solve();
	}
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值