結果
| 問題 | No.2519 Coins in Array |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-27 22:32:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 62 ms / 2,000 ms |
| コード長 | 2,059 bytes |
| 記録 | |
| コンパイル時間 | 2,278 ms |
| コンパイル使用メモリ | 207,536 KB |
| 最終ジャッジ日時 | 2025-02-17 15:24:20 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 37 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const ll INF = 9e18;
ll f(ll p, ll q) {
if (p == 0 && q == 0) return 0;
if (gcd(p, q) == 1) return p * q - p - q + 1;
return 0;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
ll best_v = INF;
vector<pii> ans;
vector<ll> arr(n);
for (ll &i : arr)
cin >> i;
auto op = [&](int a, int b) {
ans.pb(pii(a + 1, b + 1));
if (a > b) swap(a, b);
ll res = f(arr[a], arr[b]);
arr.erase(arr.begin() + b);
arr.erase(arr.begin() + a);
arr.pb(res);
};
if (SZ(arr) <= 3) {
vector<pii> best_ans;
auto _dfs = [&](auto dfs) {
if (SZ(arr) == 1) {
if (best_v > arr[0])
best_v = arr[0], best_ans = ans;
return;
}
vector<ll> tmp = arr;
for (int i = 0; i < SZ(arr); ++i)
for (int j = i + 1; j < SZ(arr); ++j) {
op(i, j);
dfs(dfs);
arr = tmp, ans.pop_back();
}
};
_dfs(_dfs);
ans.swap(best_ans);
}
else {
while (true) {
vector<int> even, odd;
for (int i = 0; i < SZ(arr); ++i)
if (arr[i] % 2 == 0)
even.pb(i);
else
odd.pb(i);
if (SZ(even) >= 2) {
op(even[SZ(even) - 2], even.back());
break;
}
else
op(odd[SZ(odd) - 2], odd.back());
}
assert(arr.back() == 0);
while (SZ(arr) > 1) {
op(SZ(arr) - 2, SZ(arr) - 1);
}
best_v = 0;
}
cout << best_v << "\n";
for (auto [a, b] : ans)
cout << a << " " << b << "\n";
}