結果

問題 No.5020 Averaging
ユーザー wanuiwanui
提出日時 2024-03-01 02:37:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 951 ms / 1,000 ms
コード長 9,016 bytes
コンパイル時間 2,880 ms
コンパイル使用メモリ 223,912 KB
実行使用メモリ 6,676 KB
スコア 81,617,538
最終ジャッジ日時 2024-03-01 02:37:53
合計ジャッジ時間 52,809 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 931 ms
6,676 KB
testcase_01 AC 907 ms
6,676 KB
testcase_02 AC 906 ms
6,676 KB
testcase_03 AC 932 ms
6,676 KB
testcase_04 AC 935 ms
6,676 KB
testcase_05 AC 929 ms
6,676 KB
testcase_06 AC 905 ms
6,676 KB
testcase_07 AC 908 ms
6,676 KB
testcase_08 AC 929 ms
6,676 KB
testcase_09 AC 931 ms
6,676 KB
testcase_10 AC 907 ms
6,676 KB
testcase_11 AC 906 ms
6,676 KB
testcase_12 AC 951 ms
6,676 KB
testcase_13 AC 932 ms
6,676 KB
testcase_14 AC 906 ms
6,676 KB
testcase_15 AC 907 ms
6,676 KB
testcase_16 AC 947 ms
6,676 KB
testcase_17 AC 932 ms
6,676 KB
testcase_18 AC 905 ms
6,676 KB
testcase_19 AC 907 ms
6,676 KB
testcase_20 AC 932 ms
6,676 KB
testcase_21 AC 935 ms
6,676 KB
testcase_22 AC 917 ms
6,676 KB
testcase_23 AC 908 ms
6,676 KB
testcase_24 AC 930 ms
6,676 KB
testcase_25 AC 934 ms
6,676 KB
testcase_26 AC 931 ms
6,676 KB
testcase_27 AC 912 ms
6,676 KB
testcase_28 AC 908 ms
6,676 KB
testcase_29 AC 929 ms
6,676 KB
testcase_30 AC 930 ms
6,676 KB
testcase_31 AC 921 ms
6,676 KB
testcase_32 AC 908 ms
6,676 KB
testcase_33 AC 932 ms
6,676 KB
testcase_34 AC 931 ms
6,676 KB
testcase_35 AC 908 ms
6,676 KB
testcase_36 AC 906 ms
6,676 KB
testcase_37 AC 930 ms
6,676 KB
testcase_38 AC 931 ms
6,676 KB
testcase_39 AC 907 ms
6,676 KB
testcase_40 AC 907 ms
6,676 KB
testcase_41 AC 929 ms
6,676 KB
testcase_42 AC 932 ms
6,676 KB
testcase_43 AC 928 ms
6,676 KB
testcase_44 AC 906 ms
6,676 KB
testcase_45 AC 930 ms
6,676 KB
testcase_46 AC 935 ms
6,676 KB
testcase_47 AC 929 ms
6,676 KB
testcase_48 AC 907 ms
6,676 KB
testcase_49 AC 907 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 avgfunc(card_t x, card_t y){
    return card_t{(x.a+y.a)/2,(x.b+y.b)/2};
}
// 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);
}
void do_op(int i, int j, vector<card_t> &now_cards, vector<pair<int, int>> &result) {
    card_t g = avgfunc(now_cards[i], now_cards[j]);
    now_cards[i] = g;
    now_cards[j] = g;
    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 anneal(vector<card_t> &now_cards, int opnum, vector<pair<int, int>> &result) {
    vector<int> ops;
    for (int i = 0; i < opnum; i++) {
        ops.push_back(i);
    }
    card_t card_zero = {0, 0};
    double old_score = 0;
    {
        for (int i = 0; i < int(ops.size()); i++) {
            card_zero.a += now_cards[ops[i]].a / (2ll << i);
            card_zero.b += now_cards[ops[i]].b / (2ll << i);
        }

        card_zero.a += now_cards[ops.back()].a / (2ll << (int(ops.size()) - 1));
        card_zero.b += now_cards[ops.back()].b / (2ll << (int(ops.size()) - 1));
        old_score = -evaluate(card_zero);
    }
    pair<double, vector<int>> bestops = {old_score, ops};
    auto add3 = [&](int sign, int xi, int yi, int zi, int x, int y, int z) {
        if (sign == 1) {
            card_zero.a += now_cards[x].a / (2ll << xi);
            card_zero.b += now_cards[x].b / (2ll << xi);
            card_zero.a += now_cards[y].a / (2ll << yi);
            card_zero.b += now_cards[y].b / (2ll << yi);
            card_zero.a += now_cards[z].a / (2ll << zi);
            card_zero.b += now_cards[z].b / (2ll << zi);
        } else {
            card_zero.a -= now_cards[x].a / (2ll << xi);
            card_zero.b -= now_cards[x].b / (2ll << xi);
            card_zero.a -= now_cards[y].a / (2ll << yi);
            card_zero.b -= now_cards[y].b / (2ll << yi);
            card_zero.a -= now_cards[z].a / (2ll << zi);
            card_zero.b -= now_cards[z].b / (2ll << zi);
        }
    };

    auto add1 = [&](int xi, int x) {
        card_zero.a += now_cards[x].a / (2ll << xi);
        card_zero.b += now_cards[x].b / (2ll << xi);
    };
    auto sub1 = [&](int xi, int x) {
        card_zero.a -= now_cards[x].a / (2ll << xi);
        card_zero.b -= now_cards[x].b / (2ll << xi);
    };
    double begin_temp = log(1e7) * 0.05;
    double end_temp = begin_temp * 0.1;
    double begin_time = marathon::now();

    int anneal_iter = 0;
    int anneal_accepted = 0;
    vector<int> candidate;
    while (marathon::now() < 900) {
        anneal_iter++;
        int xi = 0;
        int yi = 0;
        int zi = 0;
        while (xi == yi || yi == zi || zi == xi) {
            xi = marathon::randint(1, int(ops.size()) - 2);  // TODO 末尾も変更
            yi = marathon::randint(1, int(ops.size()) - 2);
            zi = marathon::randint(1, int(ops.size()) - 2);
        }
        int old_x = ops[xi];
        int old_y = ops[yi];
        int old_z = ops[zi];
        add3(-1, xi, yi, zi, old_x, old_y, old_z);
        bitset<N> used = 0;
        for (auto op : ops) {
            used[op] = 1;
        }
        used[old_x] = false;
        used[old_y] = false;
        used[old_z] = false;
        candidate.clear();
        for (int cardid = 0; cardid < N; cardid++) {
            if (!used[cardid]) candidate.push_back(cardid);
        }

        tuple<double, int, int, int> bestresult = {-1e50, -1, -1, -1};
        {
            for (auto new_x : candidate) {
                add1(xi, new_x);
                for (auto new_y : candidate) {
                    if (new_x == new_y) continue;
                    add1(yi, new_y);
                    for (auto new_z : candidate) {
                        if (new_x == new_z) continue;
                        if (new_y == new_z) continue;
                        if (old_x == new_x && old_y == new_y && old_z == new_z) continue;
                        add1(zi, new_z);
                        double score = -evaluate(card_zero);
                        if (score > get<0>(bestresult)) {
                            bestresult = {score, new_x, new_y, new_z};
                        }
                        sub1(zi, new_z);
                    }
                    sub1(yi, new_y);
                }
                sub1(xi, new_x);
            }
        }
        double new_score;
        int new_x, new_y, new_z;
        tie(new_score, new_x, new_y, new_z) = bestresult;

        if (marathon::anneal_accept(log(abs(old_score)), log(abs(new_score)), marathon::now(), begin_time, 900, begin_temp, end_temp)) {
            anneal_accepted++;
            // if (anneal_accepted % 1000 == 0) debug2(new_score, old_score);
            old_score = new_score;
            ops[xi] = new_x;
            ops[yi] = new_y;
            ops[zi] = new_z;
            add3(1, xi, yi, zi, new_x, new_y, new_z);
            if (bestops.first < new_score) {
                bestops = {new_score, ops};
            }
        } else {
            add3(1, xi, yi, zi, old_x, old_y, old_z);
        }
    }
    debug2(anneal_iter, anneal_accepted);
    for (int i = int(bestops.second.size() - 1); i > 0; i--) {
        do_op(bestops.second[i], bestops.second[i - 1], now_cards, result);
    }
}
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;
    {
        tuple<double, int, int, int> bestresult = {-1e50, -1, -1, -1};
        for (int x = 1; x < N; x++) {
            for (int y = 1; y < N; y++) {
                if (x == y) continue;
                for (int z = 1; z < N; z++) {
                    if (x == z) continue;
                    if (y == z) continue;
                    card_t c0 = now_cards[0];
                    card_t cx = now_cards[x];
                    card_t cy = now_cards[y];
                    card_t cz = now_cards[z];

                    card_t nxt_c0 = {c0.a / 2 + cx.a / 4 + cy.a / 8 + cz.a / 8, c0.b / 2 + cx.b / 4 + cy.b / 8 + cz.b / 8};
                    double score = -evaluate(nxt_c0);
                    bestresult = max(bestresult, {score, x, y, z});
                }
            }
        }
        {
            double score;
            int x, y, z;
            tie(score, x, y, z) = bestresult;
            do_op(z, y, now_cards, result);
            do_op(y, x, now_cards, result);
            do_op(x, 0, now_cards, result);
        }
    }
    debug3(evaluate(now_cards[0]), now_cards[0].a, now_cards[0].b);
    anneal(now_cards, 45, result);
    debug3(evaluate(now_cards[0]), now_cards[0].a, now_cards[0].b);
    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