結果

問題 No.5020 Averaging
ユーザー Godai TakashinaGodai Takashina
提出日時 2024-02-25 16:40:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 956 ms / 1,000 ms
コード長 7,668 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 111,848 KB
実行使用メモリ 6,548 KB
スコア 36,960,551
最終ジャッジ日時 2024-02-25 16:41:01
合計ジャッジ時間 52,292 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 955 ms
6,548 KB
testcase_01 AC 954 ms
6,548 KB
testcase_02 AC 954 ms
6,548 KB
testcase_03 AC 953 ms
6,548 KB
testcase_04 AC 954 ms
6,548 KB
testcase_05 AC 954 ms
6,548 KB
testcase_06 AC 955 ms
6,548 KB
testcase_07 AC 954 ms
6,548 KB
testcase_08 AC 954 ms
6,548 KB
testcase_09 AC 955 ms
6,548 KB
testcase_10 AC 954 ms
6,548 KB
testcase_11 AC 955 ms
6,548 KB
testcase_12 AC 956 ms
6,548 KB
testcase_13 AC 955 ms
6,548 KB
testcase_14 AC 955 ms
6,548 KB
testcase_15 AC 956 ms
6,548 KB
testcase_16 AC 954 ms
6,548 KB
testcase_17 AC 954 ms
6,548 KB
testcase_18 AC 955 ms
6,548 KB
testcase_19 AC 954 ms
6,548 KB
testcase_20 AC 954 ms
6,548 KB
testcase_21 AC 956 ms
6,548 KB
testcase_22 AC 954 ms
6,548 KB
testcase_23 AC 956 ms
6,548 KB
testcase_24 AC 955 ms
6,548 KB
testcase_25 AC 953 ms
6,548 KB
testcase_26 AC 954 ms
6,548 KB
testcase_27 AC 956 ms
6,548 KB
testcase_28 AC 954 ms
6,548 KB
testcase_29 AC 954 ms
6,548 KB
testcase_30 AC 954 ms
6,548 KB
testcase_31 AC 954 ms
6,548 KB
testcase_32 AC 956 ms
6,548 KB
testcase_33 AC 956 ms
6,548 KB
testcase_34 AC 955 ms
6,548 KB
testcase_35 AC 953 ms
6,548 KB
testcase_36 AC 956 ms
6,548 KB
testcase_37 AC 955 ms
6,548 KB
testcase_38 AC 954 ms
6,548 KB
testcase_39 AC 955 ms
6,548 KB
testcase_40 AC 955 ms
6,548 KB
testcase_41 AC 955 ms
6,548 KB
testcase_42 AC 954 ms
6,548 KB
testcase_43 AC 955 ms
6,548 KB
testcase_44 AC 956 ms
6,548 KB
testcase_45 AC 954 ms
6,548 KB
testcase_46 AC 954 ms
6,548 KB
testcase_47 AC 954 ms
6,548 KB
testcase_48 AC 955 ms
6,548 KB
testcase_49 AC 956 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <cmath>

constexpr int max_swap = 50;
static int n;
static std::mt19937 mt(0);
static std::uniform_real_distribution<> uniform(0.,1.);
static std::vector<long> a, b;

using Entry = std::pair<int, int>;
using Solution = std::vector<Entry>;

long calc_score(const Solution& ans, const std::vector<long>& a, const std::vector<long>& b) {
    std::vector<long> aa = a;
    std::vector<long> bb = b;
    for (auto [u,v] : ans) {
        long v1 = (aa[u] + aa[v])/2;
        long v2 = (bb[u] + bb[v])/2;
        aa[u] = v1;
        aa[v] = v1;
        bb[u] = v2;
        bb[v] = v2;
    }
    double v1 = std::abs(aa[0] - 5*1e17);
    double v2 = std::abs(bb[0] - 5*1e17);
    long score = 2000000L - 100000L * std::log10(std::max(v1, v2) + 1);
    return score;
}

void print_ans(const Solution& ans) {
    std::cout << ans.size() << std::endl;
    for (auto [u,v] : ans) {
        std::cout << u+1 << " " << v+1 << std::endl;
    }
}

void debug_print_ans(const Solution& ans) {
    for (auto [u,v] : ans) {
        std::cerr << "(" << u << "," << v << ") ";
    }
    std::cerr << "score = " << calc_score(ans, a, b) << std::endl;
}

auto gen_random_ans(int n) {
    constexpr int m = 50;
    std::vector<Entry> ans;
    for (int i = 0; i < m; i++) {
        int u, v;
        while (true) {
            u = mt() % n;
            v = mt() % n;
            if (u != v) {
                break;
            }
        }
        ans.emplace_back(u,v);
    }
    return ans;
}

auto greedy_ans(int n) {
    constexpr int m = 50;
    std::vector<Entry> ans;
    long best_score = calc_score(ans, a, b);
    std::cerr << "initial score: " << best_score << std::endl;
    for (int i = 0; i < m; i++) {
        /* std::cerr << i << " th iter" << std::endl; */
        int u = 0;
        int best_v = -1;
        for (int v = 1; v < n; v++) {
            ans.emplace_back(u,v);
            long score = calc_score(ans, a, b);
            /* std::cerr << "v, score: " << v << ", " << score << std::endl; */
            if (score > best_score) {
                best_score = score;
                best_v = v;
            }
            ans.pop_back();
        }
        /* std::cerr << "best v, score: " << best_v << ", " << best_score << std::endl; */
        if (best_v == -1) {
            break;
        }
        else {
            ans.emplace_back(u,best_v);
        }
    }
    return ans;
}

auto add_two_swaps(Solution& ans) {
    constexpr int m = 50;
    long best_score = calc_score(ans, a, b);
    for (int u = 1; u < n-1; u++) {
        for (int v = u+1; v < n; v++) {
            ans.push_back(Entry{u,v});
            ans.push_back(Entry{0,u});
            long score = calc_score(ans, a, b);
            if (score > best_score) {
                best_score = score;
            }
            else {
                ans.pop_back();
                ans.pop_back();
            }
        }
    }
    return ans;
}

auto greedy_iter_two_swap(int n) {
    Solution ans;
    while (ans.size() <= max_swap - 2) {
        auto size = ans.size();
        add_two_swaps(ans);
        if (size == ans.size()) {  // no pair inserted
            break;
        }
    }
    return ans;
}

void annealing(Solution& ans) {
    std::cerr << "annealing()" << std::endl;
    constexpr int m = 50;
    long best_score = calc_score(ans, a, b);
    auto start_time = std::chrono::steady_clock::now();
    const double duration = 950;  // msec
    const double start_temp = 1000;
    const double end_temp = 100;
    /* const long max_iter = 50000000; */
    /* for (long iter = 0; iter < max_iter; iter++) { */
    /*     double temp = start_temp + (end_temp - start_temp) * (static_cast<double>(iter) / max_iter); */
    while (true) {
        auto time = std::chrono::steady_clock::now();
        auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(time - start_time).count();
        if (elapsed > duration) {
            break;
        }
        double temp = start_temp + (end_temp - start_temp) * (static_cast<double>(elapsed) / duration);
        int r = mt() % 2;
        // add a random entry
        if (r == 0) {
            if (ans.size() == m) continue;
            int u = mt() % n;
            int v = mt() % n;
            if (u == v) continue;
            int pos = mt() % (ans.size() + 1);
            ans.insert(ans.begin() + pos, Entry{u,v});
            long score = calc_score(ans, a, b);
            double ratio = std::exp((score - best_score) / temp);
            double dice = uniform(mt);
            if (dice < ratio) {
                /* std::cerr << "insert (" << u << ", " << v << ")" << " at " << pos << std::endl; */
                /* std::cerr << "score: " << best_score << " -> " << score << std::endl; */
                best_score = score;
            }
            else {
                ans.erase(ans.begin() + pos);
            }
        }
        // remove a random entry
        if (r == 1) {
            if (ans.size() == 0) continue;
            int pos = mt() % ans.size();
            auto [u,v] = ans[pos];
            ans.erase(ans.begin() + pos);
            long score = calc_score(ans, a, b);
            double ratio = std::exp((score - best_score) / temp);
            double dice = uniform(mt);
            if (dice < ratio) {
                /* std::cerr << "Delete (" << u << ", " << v << ")" << " at " << pos << std::endl; */
                /* std::cerr << "score: " << best_score << " -> " << score << std::endl; */
                best_score = score;
            }
            else {
                ans.insert(ans.begin() + pos, Entry{u,v});
            }
        }
        // replace an entry with random value
        /* if (r == _) { */
        /*     if (ans.size() == 0) continue; */
        /*     int pos = mt() % ans.size(); */
        /*     int new_u = mt() % n; */
        /*     int new_v = mt() % n; */
        /*     auto [u,v] = ans[pos]; */
        /*     ans[pos] = Entry{new_u, new_v}; */
        /*     long score = calc_score(ans, a, b); */
        /*     double ratio = std::exp((score - best_score) / temp); */
        /*     double dice = uniform(mt); */
        /*     if (dice < ratio) { */
        /*         best_score = score; */
        /*     } */
        /*     else { */
        /*         ans[pos] = Entry{u,v}; */
        /*     } */
        /* } */
        // swap random two entries
        /* if (r == _) { */
        /*     if (ans.size() == 0) continue; */
        /*     auto i = mt() % ans.size(); */
        /*     auto j = mt() % ans.size(); */
        /*     if (i == j) continue; */
        /*     std::swap(ans[i], ans[j]); */
        /*     long score = calc_score(ans, a, b); */
        /*     double ratio = std::exp((score - best_score) / temp); */
        /*     double dice = uniform(mt); */
        /*     if (dice < ratio) { */
        /*         best_score = score; */
        /*     } */
        /*     else { */
        /*         std::swap(ans[i], ans[j]); */
        /*     } */
        /* } */
    }
}

int main() {
    std::cin >> n;

    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i] >> b[i];
    }

    auto ans = greedy_iter_two_swap(n);
    std::cerr << "-----------------" << std::endl;
    std::cerr << "greedy: ";
    debug_print_ans(ans);
    std::cerr << "-----------------" << std::endl;
    annealing(ans);
    std::cerr << "annealing: ";
    debug_print_ans(ans);
    std::cerr << "-----------------" << std::endl;
    std::cerr << "Final score: " << calc_score(ans, a, b) << std::endl;
    print_ans(ans);
}
0