結果

問題 No.2519 Coins in Array
ユーザー baluteshihbaluteshih
提出日時 2023-10-27 22:32:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,059 bytes
コンパイル時間 2,893 ms
コンパイル使用メモリ 214,464 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2023-10-27 22:32:47
合計ジャッジ時間 6,533 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 51 ms
7,932 KB
testcase_24 AC 54 ms
7,932 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 56 ms
6,872 KB
testcase_28 AC 55 ms
6,868 KB
testcase_29 AC 52 ms
6,872 KB
testcase_30 AC 49 ms
6,724 KB
testcase_31 AC 10 ms
4,348 KB
testcase_32 AC 26 ms
5,012 KB
testcase_33 AC 40 ms
6,400 KB
testcase_34 AC 19 ms
4,508 KB
testcase_35 AC 36 ms
5,256 KB
testcase_36 AC 14 ms
4,348 KB
testcase_37 AC 8 ms
4,348 KB
testcase_38 AC 28 ms
5,088 KB
testcase_39 AC 18 ms
4,552 KB
testcase_40 AC 47 ms
7,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0