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

E.

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

#include
#include 
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 
#include 
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 
#include 
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 
#include 
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;
}

你可能感兴趣的:(新生赛)