結果
問題 | No.5020 Averaging |
ユーザー | e869120 |
提出日時 | 2024-02-22 12:48:24 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 858 ms / 1,000 ms |
コード長 | 5,676 bytes |
コンパイル時間 | 1,099 ms |
コンパイル使用メモリ | 85,964 KB |
実行使用メモリ | 6,548 KB |
スコア | 87,307,342 |
最終ジャッジ日時 | 2024-02-25 12:52:17 |
合計ジャッジ時間 | 46,606 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 855 ms
6,548 KB |
testcase_01 | AC | 858 ms
6,548 KB |
testcase_02 | AC | 856 ms
6,548 KB |
testcase_03 | AC | 856 ms
6,548 KB |
testcase_04 | AC | 857 ms
6,548 KB |
testcase_05 | AC | 856 ms
6,548 KB |
testcase_06 | AC | 857 ms
6,548 KB |
testcase_07 | AC | 855 ms
6,548 KB |
testcase_08 | AC | 856 ms
6,548 KB |
testcase_09 | AC | 856 ms
6,548 KB |
testcase_10 | AC | 856 ms
6,548 KB |
testcase_11 | AC | 855 ms
6,548 KB |
testcase_12 | AC | 856 ms
6,548 KB |
testcase_13 | AC | 856 ms
6,548 KB |
testcase_14 | AC | 856 ms
6,548 KB |
testcase_15 | AC | 856 ms
6,548 KB |
testcase_16 | AC | 856 ms
6,548 KB |
testcase_17 | AC | 856 ms
6,548 KB |
testcase_18 | AC | 856 ms
6,548 KB |
testcase_19 | AC | 856 ms
6,548 KB |
testcase_20 | AC | 855 ms
6,548 KB |
testcase_21 | AC | 856 ms
6,548 KB |
testcase_22 | AC | 856 ms
6,548 KB |
testcase_23 | AC | 857 ms
6,548 KB |
testcase_24 | AC | 857 ms
6,548 KB |
testcase_25 | AC | 856 ms
6,548 KB |
testcase_26 | AC | 856 ms
6,548 KB |
testcase_27 | AC | 856 ms
6,548 KB |
testcase_28 | AC | 857 ms
6,548 KB |
testcase_29 | AC | 856 ms
6,548 KB |
testcase_30 | AC | 856 ms
6,548 KB |
testcase_31 | AC | 858 ms
6,548 KB |
testcase_32 | AC | 856 ms
6,548 KB |
testcase_33 | AC | 857 ms
6,548 KB |
testcase_34 | AC | 856 ms
6,548 KB |
testcase_35 | AC | 856 ms
6,548 KB |
testcase_36 | AC | 856 ms
6,548 KB |
testcase_37 | AC | 856 ms
6,548 KB |
testcase_38 | AC | 856 ms
6,548 KB |
testcase_39 | AC | 857 ms
6,548 KB |
testcase_40 | AC | 856 ms
6,548 KB |
testcase_41 | AC | 856 ms
6,548 KB |
testcase_42 | AC | 856 ms
6,548 KB |
testcase_43 | AC | 856 ms
6,548 KB |
testcase_44 | AC | 856 ms
6,548 KB |
testcase_45 | AC | 856 ms
6,548 KB |
testcase_46 | AC | 857 ms
6,548 KB |
testcase_47 | AC | 855 ms
6,548 KB |
testcase_48 | AC | 856 ms
6,548 KB |
testcase_49 | AC | 856 ms
6,548 KB |
ソースコード
#include <iostream> #include <cmath> #include <ctime> #include <vector> #include <algorithm> using namespace std; const long long MaxVal = 1000000000000000000LL; const long long MinVal = 100000000000000000LL; const long long Target = 500000000000000000LL; long long N; long long A[59], LA[59]; long long B[59], LB[59]; long long FinalScore = MaxVal; vector<int> FinalOrder; // ========================================================================================================== 基本関数 long long GetScore(vector<int>& Order) { long long TotalScore1 = (A[1] >> (N - 1)); long long TotalScore2 = (B[1] >> (N - 1)); for (int i = 0; i < N - 1; i++) TotalScore1 += (A[Order[i]] >> (N - 1 - i)); for (int i = 0; i < N - 1; i++) TotalScore2 += (B[Order[i]] >> (N - 1 - i)); return max(abs(Target - TotalScore1), abs(Target - TotalScore2)); } pair<long long, int> GetActual(vector<int>& Order) { for (int i = 1; i <= N; i++) LA[i] = A[i]; for (int i = 1; i <= N; i++) LB[i] = B[i]; for (int i = 0; i < Order.size(); i++) { long long avgA = (LA[1] + LA[Order[i]]) / 2LL; long long avgB = (LB[1] + LB[Order[i]]) / 2LL; LA[1] = avgA; LA[Order[i]] = avgA; LB[1] = avgB; LB[Order[i]] = avgB; } long long gosa1 = max(abs(Target - LA[1]), abs(Target - LB[1])); long long gosa2 = max(abs(Target - LA[1] + (A[Order[0]] >> N) - (A[Order[1]] >> N)), abs(Target - LB[1] + (B[Order[0]] >> N) - (B[Order[1]] >> N))); if (gosa1 > gosa2) return make_pair(gosa2, 2); return make_pair(gosa1, 1); } long double Randouble() { double s = 0.0, t = 1.0; for (int i = 0; i < 3; i++) { t /= 1024.0; s += 1.0 * (rand() % 1024) * t; } return s; } // ========================================================================================================== 序盤・中盤探索 vector<int> GetBestOrder() { vector<int> Order(N - 1, 0); vector<int> BestOrder(N - 1, 0); for (int i = 0; i < N - 1; i++) Order[i] = i + 2; // 準備 long long CurrentScore = GetScore(Order); long long BestScore = CurrentScore; int StartTime = clock(); // 焼きなまし法開始 while (clock() - StartTime <= 35 * CLOCKS_PER_SEC / 100) { int idx1 = rand() % (N - 1); int idx2 = rand() % (N - 1); swap(Order[idx1], Order[idx2]); long long CandScore = GetScore(Order); double Diff = log10(CurrentScore) - log10(CandScore); if (Randouble() < exp(Diff / 0.17)) { if (BestScore > CandScore) { BestScore = CandScore; BestOrder = Order; } CurrentScore = CandScore; } else { swap(Order[idx1], Order[idx2]); } } return BestOrder; } // ========================================================================================================== 終盤探索 vector<int> CurrentOrder; int StartTime; int NumLoops; int FinalTie = 0; void dfs(int mask, long long CurrentA, long long CurrentB, vector<int>& Cand, vector<int>& PostOrder) { if ((NumLoops & 1023) == 0) { if (clock() - StartTime > 85 * CLOCKS_PER_SEC / 100) return; } NumLoops += 1; if (mask == (1 << Cand.size()) - 1) { long long gosaA = (A[CurrentOrder[CurrentOrder.size() - 1]] >> N) - (A[CurrentOrder[CurrentOrder.size() - 2]] >> N); long long gosaB = (B[CurrentOrder[CurrentOrder.size() - 1]] >> N) - (B[CurrentOrder[CurrentOrder.size() - 2]] >> N); if (max(abs(Target - CurrentA), abs(Target - CurrentB)) > FinalScore + 100LL && max(abs(Target - CurrentA + gosaA), abs(Target - CurrentB + gosaB)) > FinalScore + 100LL) return; vector<int> Final; for (int i = CurrentOrder.size() - 1; i >= 0; i--) Final.push_back(CurrentOrder[i]); for (int i = 0; i < PostOrder.size(); i++) Final.push_back(PostOrder[i]); pair<long long, int> Score = GetActual(Final); if (FinalScore > Score.first) { FinalScore = Score.first; FinalTie = Score.second; FinalOrder = Final; } if (Score.first <= 1000) { Score.first += 0; } return; } // 枝刈り探索 int dep = PostOrder.size() + CurrentOrder.size() + 1; long long Border1 = 12LL * (MaxVal >> dep) / 10LL; long long Border2 = 10000LL; for (int i = 0; i < Cand.size(); i++) { if ((mask >> i) & 1) continue; long long NextA = CurrentA + (A[Cand[i]] >> dep); long long NextB = CurrentB + (B[Cand[i]] >> dep); if (NextA < Target - Border1) continue; if (NextB < Target - Border1) continue; if (NextA > Target + Border2) continue; if (NextB > Target + Border2) continue; CurrentOrder.push_back(Cand[i]); dfs(mask + (1 << i), NextA, NextB, Cand, PostOrder); CurrentOrder.pop_back(); } } int main() { // Step 1. 入力 cin >> N; for (int i = 1; i <= N; i++) cin >> A[i] >> B[i]; // Step 2. 焼きなまし法 StartTime = clock(); vector<int> BestOrder = GetBestOrder(); // Step 3. 終盤探索 for (int depth = 4; depth <= 18; depth++) { long long SumA = (A[1] >> (N - 1)); long long SumB = (B[1] >> (N - 1)); for (int i = depth; i < N - 1; i++) SumA += (A[BestOrder[i]] >> (N - 1 - i)); for (int i = depth; i < N - 1; i++) SumB += (B[BestOrder[i]] >> (N - 1 - i)); vector<int> Vec1; vector<int> Vec2; for (int i = 0; i < depth; i++) Vec1.push_back(BestOrder[i]); for (int i = depth; i < N - 1; i++) Vec2.push_back(BestOrder[i]); dfs(0, SumA, SumB, Vec1, Vec2); // cout << "# " << depth << " " << FinalScore << " " << NumLoops << endl; if (clock() - StartTime > 85 * CLOCKS_PER_SEC / 100) break; } // Step 4. 出力 vector<pair<int, int>> Answer; if (FinalTie == 2) Answer.push_back(make_pair(FinalOrder[0], FinalOrder[1])); for (int i = 0; i < FinalOrder.size(); i++) Answer.push_back(make_pair(1, FinalOrder[i])); cout << Answer.size() << endl; for (pair<int, int> ans : Answer) cout << ans.first << " " << ans.second << endl; return 0; }