题解:CF341E Candies Game
aaa_Pigeon · · 题解
观察一下样例,猜测只有初始
但是怎么构造呢?
考虑一个最简单的情况:给定
如果一直导入最小盒,则最小盒会依次变成
我们试着将
设
最终我们可以转换为
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n, a[N + 10];
vector<pair<int, int>> ans;
void ask(int x, int y) {
if (a[x] > a[y]) {
cout << "oh shit";
exit(0);
}
ans.push_back({x, y});
a[y] -= a[x]; a[x] <<= 1;
}
void _sort(int &x, int &y, int &z) {
if (a[x] > a[y]) swap(x, y);
if (a[x] > a[z]) swap(x, z);
if (a[y] > a[z]) swap(y, z);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
{
int sum = 0;
for (int i = 1; i <= n; ++i) {
sum += a[i] > 0 ? 1 : 0;
}
if (sum <= 1) {
cout << -1;
return 0;
}
}
while (true) {
int x = 0, y = 0, z = 0;
for (int i = 1; i <= n; ++i) {
if (!a[i]) continue;
if (!x) x = i;
else if (!y) y = i;
else if (!z) z = i;
else break;
}
if (!z) break;
while (true) {
_sort(x, y, z);
if (!a[x]) break;
int p = a[y] / a[x], L = 0;
while ((1 << L) - 1 < p) ++L;
for (int i = 0; i < L; ++i) {
if (p & (1 << i)) ask(x, y);
else ask(x, z);
}
}
}
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i].first << ' ' << ans[i].second << '\n';
}
return 0;
}