#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
while (n -- ) {
string str;
int a = 0, b = 0, c = 0;
cin >> str;
int len = str.size();
for (int i = 0; i < len; i ++ )
if (str[i] == 'A') a ++;
else if (str[i] == 'B') b ++;
else c ++;
if (a + c == b) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int N = 2e5 + 10;
int a[N];
vector<PII> res;
LL num[N];
int main(){
int t;
scanf("%d", &t);
while (t -- ) {
int n;
res.clear();
memset(num, 0, sizeof num);
scanf("%d", &n);
LL sum = 0;
int mx = 0;
for (int i = 1; i <= n; i ++ ) {
scanf("%d", a + i);
sum += a[i];
mx = max(mx, a[i]);
res.PB(MP(a[i], i));
}
sum = min(sum / 2, sum - mx);
if (sum == 0) {
printf("0\n");
continue;
}
res.PB(MP(0, 0));
sort(res.begin(), res.end());
for (int i = 1; i <= n; i ++ ) num[i] = num[i - 1] + res[i].FI;
int pos = lower_bound(num + 1, num + n + 1, sum) - num;
int tmp = sum - num[pos - 1];
printf("%lld\n", sum);
PII k = res.back(); res.pop_back();
res.PB(MP(k.FI - tmp, k.SE));
for (int i = 1; i <= tmp; i ++ )
printf("%d %d\n", res[pos].SE, res[n].SE);
int l = pos - 1, r = n;
while (l >= 1 && r >= pos) {
for (int i = 1; i <= min(res[l].FI, res[r].FI); i ++ )
printf("%d %d\n", res[l].SE, res[r].SE);
int mn = min(res[l].FI, res[r].FI);
res[l].FI -= mn;
res[r].FI -= mn;
if (res[l].FI == 0) l --;
if (res[r].FI == 0) r --;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef vector<int> VI;
const int N = 2e5 + 10;
int a[N];
int main(){
int t;
scanf("%d", &t);
while (t -- ) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", a + i);
VI f, b;
if (n == 1) {
printf("%d\n", a[1]);
continue;
}
if (a[1] > a[2]) f.PB(a[2]), b.PB(a[1]);
else f.PB(a[1]), b.PB(a[2]);
for (int i = 3; i <= n; i ++ ) {
if (a[i] < f.back()) f.PB(a[i]);
else b.PB(a[i]);
}
while (f.size()) {
printf("%d ", f.back());
f.pop_back();
}
for (auto t : b)
printf("%d ", t);
puts("");
}
return 0;
}
A. Casimir’s String Solitaire#include <bits/stdc++.h>using namespace std;int main(){ int n; cin >> n; while (n -- ) { string str; int a = 0, b = 0, c = 0; cin >> str; int len = str.size(); for (int i = 0; i < len; i ++