結果
問題 | No.5020 Averaging |
ユーザー |
|
提出日時 | 2024-02-25 15:19:42 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 176 ms / 1,000 ms |
コード長 | 4,357 bytes |
コンパイル時間 | 1,238 ms |
コンパイル使用メモリ | 110,792 KB |
実行使用メモリ | 6,676 KB |
スコア | 25,949,237 |
最終ジャッジ日時 | 2024-02-25 15:20:31 |
合計ジャッジ時間 | 10,669 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 50 |
ソースコード
#include <iostream> #include <vector> #include <random> #include <chrono> static int n; static std::mt19937 mt(0); 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 << 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; } void climb(Solution& ans) { std::cerr << "climb()" << std::endl; constexpr int m = 50; long best_score = calc_score(ans, a, b); for (int iter = 0; iter < 10000; iter++) { int r = mt() % 2; if (r == 0) { if (ans.size() == m) continue; // add a random swap 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); if (score >= best_score) { 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); } } if (r == 1) { int pos = mt() % ans.size(); auto [u,v] = ans[pos]; ans.erase(ans.begin() + pos); long score = calc_score(ans, a, b); if (score >= best_score) { 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}); } } } } 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_ans(n); std::cerr << "-----------------" << std::endl; std::cerr << "greedy: "; debug_print_ans(ans); std::cerr << "-----------------" << std::endl; climb(ans); std::cerr << "climb: "; debug_print_ans(ans); std::cerr << "-----------------" << std::endl; std::cerr << "Final score: " << calc_score(ans, a, b) << std::endl; print_ans(ans); }