Submission #104549

#TimeUsernameProblemLanguageResultExecution timeMemory
104549KCSCThe Big Prize (IOI17_prize)C++14
100 / 100
45 ms532 KiB
#ifndef HOME #include "prize.h" #endif #include <bits/stdc++.h> using namespace std; #ifdef HOME #include <iostream> #include <vector> #include <algorithm> using namespace std; static const int max_q = 10000; static int n; static int query_count = 0; static vector<int> g; static vector<vector<int> > rank_count; vector<int> ask(int i) { query_count++; if(query_count > max_q) { cerr << "Query limit exceeded" << endl; exit(0); } if(i < 0 || i >= n) { cerr << "Bad index: " << i << endl; exit(0); } vector<int> res(2); res[0] = rank_count[g[i] - 1][i + 1]; res[1] = rank_count[g[i] - 1][n] - res[0]; return res; } #endif void solve(int le, int ri, vector<int> lef, vector<int> rig, int &ans) { if (ans != -1) { return; } if (lef[0] + lef[1] == 0) { ans = le; return; } if (rig[0] + rig[1] == 0) { ans = ri; return; } if (le >= ri - 1) { return; } if (lef[0] + lef[1] == rig[0] + rig[1] and lef[0] == rig[0]) { return; } if (lef[0] + lef[1] < rig[0] + rig[1]) { solve(le + 1, ri, ask(le + 1), rig, ans); return; } if (lef[0] + lef[1] > rig[0] + rig[1]) { solve(le, ri - 1, lef, ask(ri - 1), ans); return; } int md = (le + ri) >> 1; vector<int> aux = ask(md); solve(le, md, lef, aux, ans); solve(md, ri, aux, rig, ans); } int find_best(int n) { int ans = -1; solve(0, n - 1, ask(0), ask(n - 1), ans); return ans; } #ifdef HOME int main() { freopen("prize.in", "r", stdin); freopen("prize.out", "w", stdout); cin >> n; g.resize(n); for(int i = 0; i < n; i++) { cin >> g[i]; if(g[i] < 1) { cerr << "Invalid rank " << g[i] << " at index " << i << endl; exit(0); } } int max_rank = *max_element(g.begin(), g.end()); rank_count.resize(max_rank + 1, vector<int>(n + 1, 0)); for(int r = 0; r <= max_rank; r++) { for(int i = 1; i <= n; i++) { rank_count[r][i] = rank_count[r][i - 1]; if(g[i - 1] == r) rank_count[r][i]++; } } for(int i = 0; i <= n; i++) for(int r = 1; r <= max_rank; r++) rank_count[r][i] += rank_count[r - 1][i]; int res = find_best(n); cout << res << endl << "Query count: " << query_count << endl; return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...