結果

問題 No.5020 Averaging
ユーザー wanuiwanui
提出日時 2024-02-25 14:34:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 1,000 ms
コード長 5,607 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 218,992 KB
実行使用メモリ 6,676 KB
スコア 32,721,532
最終ジャッジ日時 2024-02-25 14:35:13
合計ジャッジ時間 19,524 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
6,676 KB
testcase_01 AC 162 ms
6,676 KB
testcase_02 AC 443 ms
6,676 KB
testcase_03 AC 213 ms
6,676 KB
testcase_04 AC 381 ms
6,676 KB
testcase_05 AC 265 ms
6,676 KB
testcase_06 AC 263 ms
6,676 KB
testcase_07 AC 314 ms
6,676 KB
testcase_08 AC 321 ms
6,676 KB
testcase_09 AC 328 ms
6,676 KB
testcase_10 AC 323 ms
6,676 KB
testcase_11 AC 161 ms
6,676 KB
testcase_12 AC 315 ms
6,676 KB
testcase_13 AC 213 ms
6,676 KB
testcase_14 AC 418 ms
6,676 KB
testcase_15 AC 214 ms
6,676 KB
testcase_16 AC 213 ms
6,676 KB
testcase_17 AC 214 ms
6,676 KB
testcase_18 AC 239 ms
6,676 KB
testcase_19 AC 366 ms
6,676 KB
testcase_20 AC 315 ms
6,676 KB
testcase_21 AC 368 ms
6,676 KB
testcase_22 AC 214 ms
6,676 KB
testcase_23 AC 468 ms
6,676 KB
testcase_24 AC 417 ms
6,676 KB
testcase_25 AC 213 ms
6,676 KB
testcase_26 AC 321 ms
6,676 KB
testcase_27 AC 212 ms
6,676 KB
testcase_28 AC 162 ms
6,676 KB
testcase_29 AC 339 ms
6,676 KB
testcase_30 AC 263 ms
6,676 KB
testcase_31 AC 163 ms
6,676 KB
testcase_32 AC 264 ms
6,676 KB
testcase_33 AC 389 ms
6,676 KB
testcase_34 AC 163 ms
6,676 KB
testcase_35 AC 387 ms
6,676 KB
testcase_36 AC 265 ms
6,676 KB
testcase_37 AC 392 ms
6,676 KB
testcase_38 AC 314 ms
6,676 KB
testcase_39 AC 215 ms
6,676 KB
testcase_40 AC 425 ms
6,676 KB
testcase_41 AC 339 ms
6,676 KB
testcase_42 AC 366 ms
6,676 KB
testcase_43 AC 213 ms
6,676 KB
testcase_44 AC 315 ms
6,676 KB
testcase_45 AC 213 ms
6,676 KB
testcase_46 AC 329 ms
6,676 KB
testcase_47 AC 162 ms
6,676 KB
testcase_48 AC 314 ms
6,676 KB
testcase_49 AC 239 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }

struct card_t { ll a; ll b; };
bool operator==(const card_t &lhs, const card_t &rhs) { return (lhs.a == rhs.a && lhs.b == rhs.b); }
bool operator!=(const card_t &lhs, const card_t &rhs) { return !(lhs == rhs); }
bool operator<(const card_t &lhs, const card_t &rhs) {
    if (lhs.a != rhs.a){return lhs.a<rhs.a;}
    return lhs.b<rhs.b;
}
card_t avg(card_t x, card_t y){
    return card_t{(x.a+y.a)/2,(x.b+y.b)/2};
}
std::ostream &operator<<(std::ostream &os, card_t &pt) {
    string s;
    s = "(" + to_string(ll(pt.a)) + ", " + to_string(ll(pt.b)) + ")";
    return os << s;
};
// clang-format on

const int OPLIMIT = 50;
const int N = 45;
const ll F17 = 500000000000000000;
card_t INIT_AB[N];
double evaluate(card_t x) {
    double da = abs(x.a - F17);
    double db = abs(x.b - F17);
    return max(da, db) * 100 + min(da, db);
}
void rec(vector<int> &ops, vector<bool> &used, pair<double, vector<int>> &best_result, card_t used_total, card_t used_zero, int maxops, vector<card_t> &now_cards) {
    if (ops.size() >= 1) {
        auto new_zero = avg(used_total, used_zero);
        double e = evaluate(new_zero);
        if (e < best_result.first) {
            best_result = {e, ops};
        }
    }
    if (ops.size() >= maxops) {
        return;
    }
    for (int j = 0; j < N; j++) {
        if (j == 0) continue;
        if (used[j]) continue;
        used[j] = true;
        ops.push_back(j);
        card_t nxt_used_total = (used_total.a <= 0) ? now_cards[j] : avg(now_cards[j], used_total);
        rec(ops, used, best_result, nxt_used_total, used_zero, maxops, now_cards);
        ops.pop_back();
        used[j] = false;
    }
}
void rec2(vector<int> &ops, vector<bool> &used, pair<double, vector<int>> &best_result, card_t used_total, card_t used_zero, int maxops, vector<card_t> &work_cards) {
    if (ops.size() >= 1 && ops.back() != 0) {
        auto new_zero = avg(work_cards[ops.back()], work_cards[0]);
        double e = evaluate(new_zero);
        if (e < best_result.first) {
            best_result = {e, ops};
        }
    }
    if (ops.size() >= maxops) {
        return;
    }
    for (int j = 0; j < N; j++) {
        if (ops.size() == 0) {
            ops.push_back(j);
            rec2(ops, used, best_result, work_cards[j], used_zero, maxops, work_cards);
            ops.pop_back();
        } else {
            int pj = ops.back();
            if (j == pj) continue;
            card_t old_pre = work_cards[pj];
            card_t old_now = work_cards[j];
            card_t avg_ = avg(work_cards[j], work_cards[pj]);
            work_cards[pj] = avg_;
            work_cards[j] = avg_;
            ops.push_back(j);
            rec2(ops, used, best_result, used_total, used_zero, maxops, work_cards);
            ops.pop_back();
            work_cards[j] = old_now;
            work_cards[pj] = old_pre;
        }
    }
}
void do_op(int i, int j, vector<card_t> &now_cards, vector<pair<int, int>> &result) {
    card_t avg_ = avg(now_cards[i], now_cards[j]);
    now_cards[i] = avg_;
    now_cards[j] = avg_;
    result.push_back({i, j});
}
void output(vector<pair<int, int>> &result) {
    cout << result.size() << endl;
    for (auto p : result) {
        cout << p.first + 1 << " " << p.second + 1 << endl;
    }
}
void solve() {
    vector<card_t> now_cards(N);
    for (int i = 0; i < N; i++) {
        now_cards[i] = INIT_AB[i];
    }

    vector<pair<int, int>> result;
    while (result.size() <= OPLIMIT) {
        card_t init_0 = now_cards[0];
        vector<int> ops;
        vector<bool> used(N);
        pair<double, vector<int>> best_result = {evaluate(now_cards[0]) * 0.999, {}};
        int maxop = min(4, OPLIMIT - 1 - int(result.size()));
        auto work_cards = now_cards;
        rec(ops, used, best_result, {0, 0}, init_0, maxop, work_cards);
        if (best_result.second.size()) {
            for (int j = 0; j < int(best_result.second.size()) - 1; j++) {
                do_op(best_result.second[j], best_result.second[j + 1], now_cards, result);
            }
            do_op(best_result.second.back(), 0, now_cards, result);
            debug2(best_result.first, now_cards[0]);
        } else {
            break;
        }
    }
    while (result.size() <= OPLIMIT) {
        card_t init_0 = now_cards[0];
        vector<int> ops;
        vector<bool> used(N);
        pair<double, vector<int>> best_result = {evaluate(now_cards[0]) * 0.999, {}};
        int maxop = min(4, OPLIMIT - 1 - int(result.size()));
        rec2(ops, used, best_result, {0, 0}, init_0, maxop, now_cards);
        if (best_result.second.size()) {
            for (int j = 0; j < int(best_result.second.size()) - 1; j++) {
                do_op(best_result.second[j], best_result.second[j + 1], now_cards, result);
            }
            do_op(best_result.second.back(), 0, now_cards, result);
            debug2(best_result.first, now_cards[0]);
        } else {
            break;
        }
    }
    output(result);
}
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        ll a, b;
        cin >> a >> b;
        INIT_AB[i] = card_t{a, b};
    }
    solve();
    return 0;
}
0