結果

問題 No.5020 Averaging
ユーザー wanuiwanui
提出日時 2024-02-25 19:43:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 615 ms / 1,000 ms
コード長 9,756 bytes
コンパイル時間 2,901 ms
コンパイル使用メモリ 233,216 KB
実行使用メモリ 65,596 KB
スコア 76,300,350
最終ジャッジ日時 2024-02-25 19:43:39
合計ジャッジ時間 31,266 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 573 ms
64,504 KB
testcase_01 AC 488 ms
65,596 KB
testcase_02 AC 492 ms
65,596 KB
testcase_03 AC 529 ms
62,456 KB
testcase_04 AC 505 ms
63,544 KB
testcase_05 AC 560 ms
64,504 KB
testcase_06 AC 500 ms
65,592 KB
testcase_07 AC 457 ms
65,596 KB
testcase_08 AC 586 ms
64,504 KB
testcase_09 AC 435 ms
65,596 KB
testcase_10 AC 581 ms
64,504 KB
testcase_11 AC 542 ms
64,504 KB
testcase_12 AC 539 ms
62,456 KB
testcase_13 AC 483 ms
64,504 KB
testcase_14 AC 553 ms
64,504 KB
testcase_15 AC 433 ms
63,548 KB
testcase_16 AC 487 ms
62,456 KB
testcase_17 AC 533 ms
64,504 KB
testcase_18 AC 477 ms
65,596 KB
testcase_19 AC 437 ms
63,548 KB
testcase_20 AC 554 ms
62,456 KB
testcase_21 AC 443 ms
65,596 KB
testcase_22 AC 573 ms
64,504 KB
testcase_23 AC 517 ms
62,456 KB
testcase_24 AC 527 ms
64,504 KB
testcase_25 AC 440 ms
65,596 KB
testcase_26 AC 486 ms
65,596 KB
testcase_27 AC 615 ms
64,504 KB
testcase_28 AC 543 ms
64,504 KB
testcase_29 AC 558 ms
64,504 KB
testcase_30 AC 567 ms
64,504 KB
testcase_31 AC 522 ms
62,456 KB
testcase_32 AC 536 ms
62,456 KB
testcase_33 AC 441 ms
65,596 KB
testcase_34 AC 507 ms
64,504 KB
testcase_35 AC 474 ms
62,456 KB
testcase_36 AC 508 ms
62,456 KB
testcase_37 AC 555 ms
64,504 KB
testcase_38 AC 406 ms
63,548 KB
testcase_39 AC 436 ms
65,596 KB
testcase_40 AC 608 ms
64,504 KB
testcase_41 AC 531 ms
64,504 KB
testcase_42 AC 552 ms
64,504 KB
testcase_43 AC 465 ms
65,596 KB
testcase_44 AC 586 ms
64,504 KB
testcase_45 AC 441 ms
65,596 KB
testcase_46 AC 557 ms
64,504 KB
testcase_47 AC 609 ms
64,504 KB
testcase_48 AC 579 ms
64,504 KB
testcase_49 AC 549 ms
64,504 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
namespace marathon {
mt19937 engine(0);
clock_t start_time;
double now() {
    return 1000.0 * (clock() - start_time) / CLOCKS_PER_SEC;
}
void marathon_init() {
    start_time = clock();
    random_device seed_gen;
    engine.seed(seed_gen());
}
int randint(int mn, int mx) {
    int rng = mx - mn + 1;
    return mn + (engine() % rng);
}
double uniform(double x, double y) {
    const int RND = 1e8;
    double mean = (x + y) / 2.0;
    double dif = y - mean;
    double p = double(engine() % RND) / RND;
    return mean + dif * (1.0 - 2.0 * p);
}
template <typename T>
T random_choice(vector<T> &vec) {
    return vec[engine() % vec.size()];
}
bool anneal_accept(double new_score, double old_score, double cur_time, double begin_time, double end_time, double begin_temp, double end_temp) {
    const int ANNEAL_RND = 1e8;
    const double ANNEAL_EPS = 1e-6;
    double temp = (begin_temp * (end_time - cur_time) + end_temp * (cur_time - begin_time)) / (end_time - begin_time);
    return (exp((new_score - old_score) / temp) > double(engine() % ANNEAL_RND) / ANNEAL_RND + ANNEAL_EPS);
}
}  // namespace marathon
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);
}
double evaluate2(card_t x, card_t dest) {
    double da = abs(x.a - dest.a);
    double db = abs(x.b - dest.b);
    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 target, int lastind, int maxops, vector<card_t> &now_cards) {
    if (ops.size() >= 1) {
        auto new_zero = avg(used_total, now_cards[lastind]);
        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 (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, target, lastind, maxops, now_cards);
        ops.pop_back();
        used[j] = false;
    }
}
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;
    }
}

vector<int> anneal(int siz, vector<card_t> &now_cards) {
    vector<int> ops;
    auto avgscore = [&]() {
        int m = ops.size();
        ll a = 0;
        ll b = 0;
        if (ops.size() == 1) {
            return -evaluate(avg(now_cards[ops.back()], now_cards[0]));
        } else {
            for (int i = 0; i < m; i++) {
                if (i == m - 1) {
                    a += now_cards[ops[i]].a / (2ll << (m - 2));
                    b += now_cards[ops[i]].b / (2ll << (m - 2));
                } else {
                    a += now_cards[ops[i]].a / (2ll << i);
                    b += now_cards[ops[i]].b / (2ll << i);
                }
            }
            return -evaluate({(a + now_cards[0].a) / 2, (b + now_cards[0].b) / 2});
        }
    };
    pair<double, vector<int>> bestresult = {-1e50, {}};
    for (int loop = 0; loop < 1000; loop++) {
        vector<bool> used(N);
        double bs = -1;
        for (int it = 0; it < siz; it++) {
            pair<double, int> best = {-1e50, -1};
            for (int u = 1; u < N; u++) {
                if (used[u]) continue;
                ops.push_back(u);
                pair<double, int> now = {avgscore() * marathon::uniform(1.0, 2.0), u};
                best = max(now, best);
                ops.pop_back();
            }
            {
                bs = best.first;
                used[best.second] = true;
                ops.push_back(best.second);
            }
        }
        if (bestresult.first < bs) {
            bestresult = {bs, ops};
            debug1(bestresult.first);
        }
        ops.clear();
    }

    return bestresult.second;
}
struct state_t {
    card_t cards[N];
    int ops[50];
    bitset<N> used;
    ll a;
    ll b;
    double score;
    int ops_size;
};
bool operator<(const state_t &lhs, const state_t &rhs) {
    return lhs.score < rhs.score;
}
void beam_swap(vector<state_t> &stats, vector<state_t> &nstats, int beamsize) {
    int nst_num = nstats.size();
    vector<int> perm(nst_num);
    iota(perm.begin(), perm.end(), 0);
    sort(perm.begin(), perm.end(), [&](int a, int b) {
        return nstats[a].score > nstats[b].score;
    });
    unordered_set<ull> hashes;
    stats.clear();
    for (int i = 0; i < nst_num; i++) {
        auto nst = nstats[perm[i]];

        stats.push_back(nst);
        if (int(stats.size()) >= beamsize) {
            break;
        }
    }
}
vector<int> beamsearch(int opnum, vector<card_t> &now_cards) {
    state_t init_state;
    {
        init_state.used = 0;
        init_state.used[0] = 1;
        init_state.ops_size = 0;
        for (int u = 0; u < N; u++) {
            init_state.cards[u] = now_cards[u];
        }
        init_state.a = 0;
        init_state.b = 0;
        init_state.score=-evaluate(now_cards[0]);
    }
    /*
                for (int i = 0; i < m; i++) {
                if (i == m - 1) {
                    a += now_cards[ops[i]].a / (2ll << (m - 2));
                    b += now_cards[ops[i]].b / (2ll << (m - 2));
                } else {
                    a += now_cards[ops[i]].a / (2ll << i);
                    b += now_cards[ops[i]].b / (2ll << i);
                }
            }
            return -evaluate({(a + now_cards[0].a) / 2, (b + now_cards[0].b) / 2});
    */
    state_t beststate = init_state;
    int beamwidth = 800;
    vector<state_t> stats = {init_state};
    auto card0 = now_cards[0];
    for (int op = 0; op < opnum; op++) {
        debug2(op, stats.size());
        vector<state_t> nstats;
        for (auto state : stats) {
            for (int i = 1; i < N; i++) {
                if (state.used[i]) continue;
                auto nst = state;
                nst.a += now_cards[i].a / (2ll << op);
                nst.b += now_cards[i].b / (2ll << op);

                nst.score = -evaluate({(nst.a + F17/(2ll<<op)+card0.a) / 2, (nst.b + F17/(2ll<<op)+card0.b) / 2});
                nst.ops[op] = i;
                nst.ops_size = op + 1;
                nst.used[i] = true;

                if (beststate.score < nst.score) {
                    beststate = nst;
                }
                if ((nst.a + card0.a) / 2 > F17) continue;
                if ((nst.b + card0.b) / 2 > F17) continue;
                nstats.push_back(nst);
            }
        }
        debug2(stats.size(), nstats.size());
        beam_swap(stats, nstats, beamwidth);
    }
    {
        vector<int> ops;
        for (int i = 0; i < beststate.ops_size; i++) {
            ops.push_back(beststate.ops[i]);
        }
        return ops;
    }
}
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;
    int it = 0;
    {
        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, 0, maxop, work_cards);
        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(evaluate(now_cards[0]), now_cards[0]);
    {
        auto ops = beamsearch(43, now_cards);
        int m = ops.size();
        if (m > 0) {
            for (int i = m - 1; i >= 1; i--) {
                do_op(ops[i], ops[i - 1], now_cards, result);
            }
            do_op(0, ops[0], now_cards, result);
        }
    }
    debug2(evaluate(now_cards[0]), now_cards[0]);
    debug1(marathon::now());
    output(result);
}
int main() {
    marathon::marathon_init();
    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