Codeforces Round #732 (Div. 2)
作者:互联网
Codeforces Round #732 (Div. 2)
A - AquaMoon and Two Arrays
模拟
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int _;
for (cin >> _; _; --_) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto &i : a)
cin >> i;
vector<pair<int, int>> ans;
stack<int> x, y;
for (int i = 0; i < n; ++i) {
cin >> b[i];
if (a[i] < b[i])
x.push(i);
else if (a[i] > b[i])
y.push(i);
}
while (!x.empty() && !y.empty()) {
while (a[x.top()] < b[x.top()] && a[y.top()] > b[y.top()]) {
ans.emplace_back(y.top(), x.top());
++a[x.top()];
--a[y.top()];
}
if (a[x.top()] == b[x.top()])
x.pop();
if (a[y.top()] == b[y.top()])
y.pop();
}
if (!x.empty() || !y.empty())
cout << "-1\n";
else {
cout << ans.size() << '\n';
for (auto &i : ans)
cout << i.first + 1 << ' ' << i.second + 1 << '\n';
}
}
return 0;
}
B - AquaMoon and Stolen String
不管怎么交换,每个位置上每种字母的个数不会变
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int _;
for (cin >> _; _; --_) {
int n, m;
cin >> n >> m;
vector<vector<int>> cnt(m, vector<int>(26, 0));
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
for (int j = 0; j < m; ++j)
++cnt[j][s[j] - 'a'];
}
for (int i = 1; i < n; ++i) {
string s;
cin >> s;
for (int j = 0; j < m; ++j)
--cnt[j][s[j] - 'a'];
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < 26; ++j)
if (cnt[i][j] == 1)
cout << char('a' + j);
cout << '\n';
}
return 0;
}
C - AquaMoon and Strange Sort
每个数最终的位置是个区间,切区间内数相同
不管怎么交换,最后的方向只和最初位置和最终位置的步数差有关,奇数向左,反之亦然
向左的时候我们就要找一个区间内也是向左的一个数且二者距离差为奇数,即可交换,使得二者全部向右
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int _;
for (cin >> _; _; --_) {
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i].first, a[i].second = i;
sort(a.begin(), a.end());
bool f = 0;
for (int i = 0, j = 0; i < n && !f; i = j) {
int val[2] = { 0, 0 };
for (; j < n && a[i].first == a[j].first; ++j)
if (abs(a[j].second - j) & 1)
++val[j & 1];
f = val[1] ^ val[0];
}
cout << (f ? "NO" : "YES") << '\n';
}
return 0;
}
D - AquaMoon and Chess
跳棋,只要是两个相邻的\(1\),就可以走到任何位置,
eg:
0 1 0 [1 1]
-> 0 1 [1 1] 0
-> 1 1 0 1 0 = [1 1] 0 1 0
发现了吗,\([0 \ 1 \ 0]\)的相对位置没变, 只是\([1 \ 1]\)插入到\(0\)的两边而已,是个组合数学
设有\(n\)对\([1 \ 1]\)(不重叠),\(m\)个\(0\),由于\(0\)的左右对称,故答案为
\(ans = C^{n}_{n+m}\)
int fac[N], inv[N], facinv[N], _, n;
int C(int n, int m) {
return (long long)fac[n] * facinv[m] % mod * facinv[n - m] % mod;
}
void init(int n) {
fac[0] = fac[1] = facinv[0] = facinv[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= n; ++i) {
fac[i] = (long long)fac[i - 1] * i % mod;
inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod;
facinv[i] = (long long)facinv[i - 1] * inv[i] % mod;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
init(1e5);
for (cin >> _; _; --_) {
cin >> n;
string s;
cin >> s;
int x = 0, y = 0;
for (int i = 0, f = 0; i < n; ++i) {
if (s[i] == '0')
++y, f = 0;
else if (f)
++x, f = 0;
else
f = 1;
}
cout << C(x + y, x) << '\n';
}
return 0;
}
标签:cout,int,top,Codeforces,cin,++,732,tie,Div 来源: https://www.cnblogs.com/2aptx4869/p/15010655.html