A
构造题
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5+5;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve() {
int n; cin >> n;
vector<int>a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int cnt1 = a[0], cnt2 = a[n-1];
int fg1 = 0, fg2 = 0;
for (int i = 0; i < n; i++) {
if (a[i] != cnt1 && a[i] != cnt2) {
cout << "No\n";
return;
}
else if (a[i] == cnt1) {
fg1++;
}
else
fg2++;
}
if (fg1 == 0 || fg2 == 0) {
cout << "Yes" << '\n';
}
else if (abs(fg1 - fg2) == 1 || abs(fg1 - fg2) == 0) {
cout << "Yes" << '\n';
}
else
cout << "No\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
B
模拟
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5+5;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve() {
int n, m; cin >> n >> m;
string s1, s2; cin >> s1 >> s2;
bool fg1 = 0, fg2 = 0;
for (int i = 1; i < n; i++) {
if (s1[i] == s1[i - 1]) {
fg1 = 1;
break;
}
}
for (int i = 1; i < m; i++) {
if (s2[i] == s2[i - 1]) {
fg2 = 1;
break;
}
}
if (s2[0] != s2[m - 1])
fg2 = 1;
if (!fg1) {
cout << "Yes\n";
return;
}
if ((fg1 && fg2)||(fg1&&m==2)) {
cout << "No\n";
return;
}
bool s1t1 = 0, s1t2 = 0, s2t1 = 0, s2t2 = 0;
if (s2[0] == s2[m - 1] && s2[0] == '0') {
s2t1 = 1;
}
else
s2t2 = 1;
for (int i = 1; i < n; i++) {
if (s1[i] == s1[i - 1]&&s1[i]=='0') {
s1t1 = 1;
}
else if (s1[i] == s1[i - 1] && s1[i] == '1') {
s1t2 = 1;
}
}
if (s1t1 && s1t2) {
cout << "No\n";
return;
}
if (s2t1) {
if (s1t1) {
cout << "No\n";
return;
}
else {
cout << "Yes\n";
return;
}
}
else {
if (s1t2) {
cout << "No\n";
return;
}
else {
cout << "Yes\n";
return;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
C
模拟,注意k不能为奇数,字符串里01数量相同,1相同在左边的1前面加01,0相同在右边0的后面加01,注意边界维护
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5+5;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve() {
int k; cin >> k;
string s; cin >> s;
string t = "01";
int cnt0 = count(s.begin(), s.end(), '0');
int cnt1 = k - cnt0;
if ((cnt1 != cnt0) || k & 1) {
cout << "-1\n";
return;
}
vector<int>ans;
int L = 0, R = k - 1;
while (L < R) {
if (s[L] != s[R]) {
L++, R--;
continue;
}
if (s[L] == '0') {
ans.push_back(R + 1);
s.insert(R + 1, t);
R += 2;
}
else {
ans.push_back(L);
s.insert(L, t);
R += 2;
}
}
cout << ans.size() << '\n';
for (auto i : ans) {
cout << i << ' ';
}
cout << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
D
可以推出如果初始时候,存在i和j不为1的点使得ai+aj>=ijc,那么a1一定至少能与其中一点连边
那么我们用升序优先队列来存,只要a1+ai-i*c>=0就可以插到a[i]上,插入后可以把两个点合并为a[1],从ai-i*c最大开始,如果队列空就可以
#include<bits/stdc++.h>
#define int ll
using namespace std;
using ll = long long;
const int maxn = 2e5+5;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve() {
int n, c; cin >> n >> c;
vector<int>a(n);
priority_queue<pair<int, int>>q;
for (int i = 0; i < n; i++) {
cin >> a[i];
if(i!=0)
q.push({ a[i] - (i+1) * c,a[i] });
}
int t = a[0];
while (!q.empty() && t + q.top().first >= 0) {
t += q.top().second;
q.pop();
}
if (q.empty())
cout << "Yes\n";
else
cout << "No\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}