新生赛002题解-“我不做人辣”

E.

在比赛时,一直卡在这个题,思想简单,但是条件一定要谨慎,稍不留神就考虑不全

#include<iostream>
#include <algorithm>
using namespace std;

bool cmp(int x, int y){
	return x > y;
}

const int MAXN = 4e5 + 10;
int a[MAXN];

int main(){
	int t;
	cin >> t;
	while (t--){
		int n, b[MAXN], count = 0, c = 1;
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}
		sort(a, a + n, cmp);
		for (int i = 1; i <= n / 2; i++){
			if (a[i] == a[i - 1]){
				c++;
			}
			else{
				b[count++] = c;
				c = 1;
			}
		}
		bool s = false;
		for (int i = 2; i < count; i++){
			if (b[1] <= b[0]){
				b[1] += b[i];
				b[i] = 0;
			}
			else if(i == 2){
				if (b[2] > b[0]){
					s = true;
				}
				continue;
			}
			else{
				b[2] += b[i];
				b[i] = 0;
				if (b[2] > b[0]){
					s = true;
				}
			}
		}
		if(s == false || count < 3){
			cout << "0 0 0" << endl;
		}
		else 
			cout << b[0] << " " << b[1] << " " << b[2] << endl;
	}
	return 0;
} 

G.

很简单,理解题目意思基本秒出,大意为在原字符串01的任何位置插入01形成新的字符串,给你n个字符串,判断他们符不符合规

#include <iostream>
#include <string>
using namespace std;

int main(){
	string s;
	int n;
	cin >> n;
	while(n--){
		int n = 0;
		cin >> s;
		bool y = false;
		for (int i =0; i < s.length(); i++){
			if (s[i] == '0')	n++;
			else
				n--;
			if (n < 0){
				y = true;
				break;
			}
		}
		if(y)	cout << "NO" << endl;
		else cout << "YES" << endl;
	}
	return 0;
}

H.

扫一遍,设置一下输出不可以的条件和“?”的变换条件
需要注意的是,循环一定要设置为小于s.length(),s的最后是‘\0’所以不会超出

#include <iostream>
#include <string>
using namespace std;

int main(){
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		bool f = true;
		for (int i = 0; i < s.length(); i++){
			if (s[i] == '?'){
				if (i == 0){
					switch (s[i + 1]){
						case 'a':
							s[i] = 'b';	break;
						case 'b':
							s[i] = 'a'; break;
						case 'c':
							s[i] = 'a'; break;
					}
				}
				if (s[i + 1] != 'c' && s[i - 1] != 'c')	s[i] = 'c';
				else if (s[i + 1] != 'b' && s[i - 1] != 'b')	s[i] = 'b';
				else if (s[i + 1] != 'a' && s[i - 1] != 'a')	s[i] = 'a';
			}
			else if(s[i] == s[i + 1]){
				f = false;
			}
		}
		if (f){
			cout << s << endl;
		}
		else
			cout << "-1" << endl;
	}
	return 0;
}

I.

看的晨曦大佬的题解,感觉有了新的思路吧,平时dfs也很少用

#include <iostream>
#include <cstdio>
using namespace std;

int n, ans;

void dfs(int a, int b){
	if (!b){
		if (a <= n && a){
			ans++;
		}
		return ;
	}
	dfs(a * 10, b-1);
	dfs(a * 10 + 1, b-1);
}

int main(){
	while(~scanf("%d", &n)){
		ans = 0;
		dfs(0, 10);
		printf("%d\n", ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值