Codeforces Round #646 (Div. 2)
题目链接:Codeforces Round #646 (Div. 2)
A. Odd Selection
代码
#include<iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, a, sum = 0, Odd = 0, Oeve = 0;
cin >> n >> x;
for (int i = 0; i < n; i++) {
cin >> a;
if (a % 2)
Odd++;
else
Oeve++;
sum += a;
}
if (n == x && sum % 2 || (n != x && (Odd && Oeve || x % 2 && Odd))) {
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
return 0;
}
B. Subsequence Hate
题意: 给你一段只有0或1的字符串,你每次可以反转一个字符,问你至少反转多少次可以得出一个没有"101"或"010’'的字符。
题解: 修改后的字符串只有:全是0,全是1,一边是0一边是1这三种结果,只要遍历所有的字符,找出最小值。
代码
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
int sum_1 = 0;
int sum_0 = 0;
int ans_1 = 0;
int ans_0 = 0;
int ans = 1100000;
for(int i = 0; i < s.size(); i++) {
if(s[i] == '0')
sum_0++;
else
sum_1++;
}
ans = min(sum_1,sum_0);
for(int i = 0; i <s.size(); i++) {
if(s[i] == '0') {
sum_0--;
ans_0++;
} else {
sum_1--;
ans_1++;
}
ans = min(ans,min(ans_1,ans_0)+min(sum_1,sum_0));
}
cout << ans << endl;
}
}
C. Game On Leaves
题意:Ayush和Ashish 两个人摘叶子,谁最后摘到编号为x的谁就赢了
题解:当节点x的度数大于等于2时,除了上图中的那三个点,还剩n-3个点,若n-3为偶数,那么最终肯定是后摘的那个人赢;反之就是先摘的那个人赢;当节点x的度数小于2时,那一定是先摘的那个人赢。
代码:
#include <iostream>
using namespace std;
int main() {
int t, n, x, a, b;
cin >> t;
while(t--) {
int sum = 0;
cin >> n >> x;
for(int i = 0; i < n-1; i++) {
cin >> a >> b;
if(a == x || b == x)
sum++;
}
if(sum <= 1)
cout << "Ayush" << endl;
else {
if((n-3)%2 == 0)
cout << "Ashish" << endl;
else
cout << "Ayush" << endl;
}
}
return 0;
}