結果
問題 | No.5020 Averaging |
ユーザー | tabae326 |
提出日時 | 2024-02-25 15:09:24 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 703 ms / 1,000 ms |
コード長 | 11,674 bytes |
コンパイル時間 | 5,013 ms |
コンパイル使用メモリ | 276,448 KB |
実行使用メモリ | 6,676 KB |
スコア | 61,717,742 |
最終ジャッジ日時 | 2024-02-25 15:10:07 |
合計ジャッジ時間 | 42,447 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 702 ms
6,676 KB |
testcase_01 | AC | 702 ms
6,676 KB |
testcase_02 | AC | 702 ms
6,676 KB |
testcase_03 | AC | 702 ms
6,676 KB |
testcase_04 | AC | 701 ms
6,676 KB |
testcase_05 | AC | 702 ms
6,676 KB |
testcase_06 | AC | 702 ms
6,676 KB |
testcase_07 | AC | 702 ms
6,676 KB |
testcase_08 | AC | 702 ms
6,676 KB |
testcase_09 | AC | 702 ms
6,676 KB |
testcase_10 | AC | 701 ms
6,676 KB |
testcase_11 | AC | 702 ms
6,676 KB |
testcase_12 | AC | 702 ms
6,676 KB |
testcase_13 | AC | 702 ms
6,676 KB |
testcase_14 | AC | 702 ms
6,676 KB |
testcase_15 | AC | 702 ms
6,676 KB |
testcase_16 | AC | 703 ms
6,676 KB |
testcase_17 | AC | 702 ms
6,676 KB |
testcase_18 | AC | 702 ms
6,676 KB |
testcase_19 | AC | 702 ms
6,676 KB |
testcase_20 | AC | 702 ms
6,676 KB |
testcase_21 | AC | 702 ms
6,676 KB |
testcase_22 | AC | 702 ms
6,676 KB |
testcase_23 | AC | 702 ms
6,676 KB |
testcase_24 | AC | 701 ms
6,676 KB |
testcase_25 | AC | 702 ms
6,676 KB |
testcase_26 | AC | 703 ms
6,676 KB |
testcase_27 | AC | 703 ms
6,676 KB |
testcase_28 | AC | 702 ms
6,676 KB |
testcase_29 | AC | 702 ms
6,676 KB |
testcase_30 | AC | 702 ms
6,676 KB |
testcase_31 | AC | 702 ms
6,676 KB |
testcase_32 | AC | 702 ms
6,676 KB |
testcase_33 | AC | 702 ms
6,676 KB |
testcase_34 | AC | 702 ms
6,676 KB |
testcase_35 | AC | 702 ms
6,676 KB |
testcase_36 | AC | 702 ms
6,676 KB |
testcase_37 | AC | 702 ms
6,676 KB |
testcase_38 | AC | 703 ms
6,676 KB |
testcase_39 | AC | 701 ms
6,676 KB |
testcase_40 | AC | 703 ms
6,676 KB |
testcase_41 | AC | 702 ms
6,676 KB |
testcase_42 | AC | 702 ms
6,676 KB |
testcase_43 | AC | 702 ms
6,676 KB |
testcase_44 | AC | 702 ms
6,676 KB |
testcase_45 | AC | 703 ms
6,676 KB |
testcase_46 | AC | 702 ms
6,676 KB |
testcase_47 | AC | 702 ms
6,676 KB |
testcase_48 | AC | 702 ms
6,676 KB |
testcase_49 | AC | 702 ms
6,676 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using ll = long long; using pl = std::pair<long long, long long>; //using mint = atcoder::modint1000000007; using mint = atcoder::modint998244353; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) #define all(v) (v).begin(), (v).end() #define dump(var) do{ if(debug::enable) { std::cerr << #var << " : "; debug::print(var); } } while(0); constexpr int di[4] = {1, -1, 0, 0}; constexpr int dj[4] = {0, 0, 1, -1}; constexpr long long inf = 1LL<<60; template<typename T> inline void chmax(T &a, T b) { a = std::max(a, b); }; template<typename T> inline void chmin(T &a, T b) { a = std::min(a, b); }; template<typename T> void vprint(const std::vector<T>& v) { for(int i = 0; i < v.size(); i++) { std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); } } template<typename T> void vprintln(const std::vector<T>& v) { for(int i = 0; i < v.size(); i++) { std::cout << v[i] << "\n"; } } template<int M> void vprint(const std::vector<atcoder::static_modint<M>>& v) { for(int i = 0; i < v.size(); i++) { std::cout << v[i].val() << (i == v.size()-1 ? "\n" : " "); } } template<int M> void vprintln(const std::vector<atcoder::static_modint<M>>& v) { for(int i = 0; i < v.size(); i++) { std::cout << v[i].val() << "\n"; } } namespace debug { #if defined(ONLINE_JUDGE) const bool enable = false; #else const bool enable = true; #endif template<typename T> void push(const T& e) { std::cerr << e; } template<int M> void push(const atcoder::static_modint<M>& e) { std::cerr << e.val(); } template<typename T1, typename T2> void push(const std::pair<T1, T2>& e) { std::cerr << "("; push(e.first); std::cerr << ","; push(e.second); std::cerr << ")"; } template<typename T1, typename T2, typename T3> void push(const std::tuple<T1, T2, T3>& e) { std::cerr << "("; push(get<0>(e)); std::cerr << ","; push(get<1>(e)); std::cerr << ","; push(get<2>(e)); std::cerr << ")"; } template<typename T> void print(const T& e) { push(e); std::cerr << "\n"; } template<typename T> void print(const std::vector<T>& v) { for(int i = 0; i < v.size(); i++) { push(v[i]); std::cerr << " "; } std::cerr << "\n"; } template<typename T> void print(const std::vector<std::vector<T>>& v) { std::cerr << "\n"; for(int i = 0; i < v.size(); i++) { std::cerr << i << ": "; print(v[i]); } } }; struct RandGenerator { random_device seed_gen; mt19937 engine; mt19937_64 engine64; static const int pshift = 1000000000; RandGenerator() : engine(seed_gen()), engine64(seed_gen()) {} /*mod以下の乱数を返す(32bit)*/ int rand(int mod) { return engine() % mod; } /*mod以下の乱数を返す(64bit)*/ long long randll(long long mod) { return engine64() % mod; } /*確率pでTrueを返す*/ bool pjudge(double p) { int p_int; if(p > 1) p_int = pshift; else p_int = p * pshift; return rand(pshift) < p_int; } } ryuka; #include <sys/time.h> struct timer { double global_start; double gettime() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec + tv.tv_usec * 1e-6; } void init() { global_start = gettime(); } double elapsed() { return gettime() - global_start; } } toki; constexpr ll TARGET = 500000000000000000; ll N; vector<pl> V; struct State { vector<long long> idx; long long end_pos; long long score; State() : score(-inf) {}; long long calc_score(); static State initState(); static State generateState(const State& input_state); }; template<class STATE> struct IterationControl { int iteration_counter; int swap_counter; double average_time; double start_time; IterationControl() : iteration_counter(0), swap_counter(0) {} STATE climb(double time_limit, STATE initial_state); STATE anneal(double time_limit, double temp_start, double temp_end, STATE initial_state); }; // 2べき判定 bool is_2beki(ll val) { ll cnt = 0; while(val) { cnt += val % 2; val /= 2; } return cnt == 1; } // 与えられた集合に対して、全パターンを試し、 // 最もTARGETに近くなるパターンと、その誤差を返す。 pair<ll, vector<ll>> search_lowest_diff_pattern(const vector<ll>& idx) { const ll size = idx.size(); ll best_diff = inf; ll best_mask = -1; rep(mask, 0, 1<<size) { // 0が含まれていなければスキップ if(!(mask & 1)) continue; unsigned long long sum_front = 0; unsigned long long sum_back = 0; rep(i, 1, size) { if(mask & (1<<i)) { sum_front = (sum_front + V[idx[i]].first) / 2; sum_back = (sum_back + V[idx[i]].second) / 2; } } ll max_ave = max(sum_front / size, sum_back / size); if(abs(TARGET - max_ave) < best_diff) { best_diff = abs(TARGET - max_ave); best_mask = mask; } } assert(best_mask != -1); vector<ll> res; rep(i, 0, size) if(best_mask & (1<<i)) res.push_back(i); return {best_diff, res}; } // 前から順に足していって、もっとも誤差が小さくなるタイミングを返す pl calc_max_diff(const vector<ll>& idx) { const ll size = idx.size(); ll sum_front = V[idx[0]].first; ll sum_back = V[idx[0]].second; ll min_diff = inf, min_i = 0; rep(i, 1, size) { sum_front = (sum_front + V[idx[i]].first) / 2; sum_back = (sum_back + V[idx[i]].second) / 2; ll diff = max(abs(TARGET - sum_front), abs(TARGET - sum_back)); if(diff < min_diff) { min_diff = diff; min_i = i; } } return {min_diff, min_i}; } // ビット全探索ができるサイズ N=10くらいまで、ピックアップして、 // 平均値をなるべく5*10^17に近づけようとしてみる // N=10の場合、unsiged long longならばオーバーフローしない。 vector<ll> decide_indicies_to_use(const double time_limit = 0.6) { const double start_time = toki.gettime(); ll best_diff = inf; vector<ll> best_idx_to_use; vector<ll> idx(N); iota(all(idx), 0); ll counter = 0; while(toki.gettime() - start_time < time_limit) { counter++; shuffle(all(idx), ryuka.engine); rep(i, 0, idx.size()) { if(idx[i] == 0 && i != 0) { swap(idx[i], idx[0]); break; } } auto [diff, pos] = calc_max_diff(idx); if(diff < best_diff) { best_diff = diff; best_idx_to_use = vector<ll>(idx.begin(), idx.begin()+pos+1); } } dump(counter); dump(toki.gettime() - start_time); assert(!best_idx_to_use.empty()); assert(best_idx_to_use[0] == 0); return best_idx_to_use; } // 順々に足す vector<pl> add_ordinally(const vector<ll>& idx) { vector<pl> ans; rep(i, 1, idx.size()) ans.push_back({0, idx[i]}); return ans; } // トーナメント方式でたす vector<pl> add_uniformly(const vector<ll>& idx) { assert(idx[0] == 0); assert(is_2beki(idx.size())); vector<pl> debug_v = V; vector<pl> ans; for(ll b = 0; (1<<b) < idx.size(); b++) { for(ll i = 0; i < idx.size(); i += (1<<b) * 2) { ll u = idx[i]; ll v = idx[i + (1<<b)]; ans.push_back({u, v}); ll ave_front = (debug_v[u].first + debug_v[v].first) / 2; ll ave_back = (debug_v[u].second + debug_v[v].second) / 2; debug_v[u] = debug_v[v] = {ave_front, ave_back}; } } dump(debug_v); return ans; } void print_ans(const vector<pl>& ans) { cout << ans.size() << endl; for(auto [u, v]: ans) { cout << u+1 << " " << v+1 << endl; } } long long State::calc_score() { ll a = V[idx[0]].first, b = V[idx[0]].second; rep(i, 1, end_pos) { a = (a + V[idx[i]].first) / 2; b = (b + V[idx[i]].second) / 2; } ll diff = max(abs(a - TARGET), abs(b - TARGET)); this->score = 2000000 - 100000 * log10(diff+1); return this->score; } State State::initState() { State res; res.idx.resize(N); iota(all(res.idx), 0); shuffle(res.idx.begin()+1, res.idx.end(), ryuka.engine); auto [_, pos] = calc_max_diff(res.idx); res.end_pos = pos + 1; res.calc_score(); return res; } State State::generateState(const State& input_state) { State res = input_state; ll i = ryuka.rand(N-1) + 1; ll j = ryuka.rand(N) + 1; if(i > j) swap(i, j); if(i == j) j++; shuffle(res.idx.begin() + i, res.idx.begin() + j, ryuka.engine); auto [_, pos] = calc_max_diff(res.idx); res.end_pos = pos + 1; assert(res.idx[0] == 0); res.calc_score(); return res; } template<class STATE> STATE IterationControl<STATE>::climb(double time_limit, STATE initial_state) { start_time = toki.gettime(); average_time = 0; STATE best_state = initial_state; double time_stamp = start_time; cerr << "[INFO] - IterationControl::climb - Starts climbing...\n"; while(time_stamp - start_time + average_time < time_limit) { STATE current_state = STATE::generateState(best_state); if(current_state.score > best_state.score) { swap(best_state, current_state); swap_counter++; } iteration_counter++; time_stamp = toki.gettime(); average_time = (time_stamp - start_time) / iteration_counter; } cerr << "[INFO] - IterationControl::climb - Iterated " << iteration_counter << " times and swapped " << swap_counter << " times.\n"; return best_state; } template<class STATE> STATE IterationControl<STATE>::anneal(double time_limit, double temp_start, double temp_end, STATE initial_state) { start_time = toki.gettime(); average_time = 0; STATE best_state = initial_state; double elapsed_time = 0; cerr << "[INFO] - IterationControl::anneal - Starts annealing...\n"; while(elapsed_time + average_time < time_limit) { double normalized_time = elapsed_time / time_limit; double temp_current = pow(temp_start, 1.0 - normalized_time) * pow(temp_end, normalized_time); STATE current_state = STATE::generateState(best_state); long long delta = current_state.score - best_state.score; if(delta > 0 || ryuka.pjudge(exp(1.0 * delta / temp_current)) ) { swap(best_state, current_state); swap_counter++; } iteration_counter++; elapsed_time = toki.gettime() - start_time; average_time = elapsed_time / iteration_counter; } cerr << "[INFO] - IterationControl::anneal - Iterated " << iteration_counter << " times and swapped " << swap_counter << " times.\n"; return best_state; } void solve() { // 入力を受け取る。 cin >> N; V.resize(N); for(auto& [a, b]: V) cin >> a >> b; // 実際に使うインデックスを決定する。 // vector<ll> idx_to_use = decide_indicies_to_use(); IterationControl<State> sera; State stat = sera.anneal(0.7, 100000, 100, State::initState()); dump(stat.score); vector<ll> idx_to_use = vector<ll>(stat.idx.begin(), stat.idx.begin()+stat.end_pos); dump(idx_to_use); // 平均をとるように加算していく vector<pl> ans = add_ordinally(idx_to_use); // 出力 print_ans(ans); } int main() { toki.init(); ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }