Submission #765703

#TimeUsernameProblemLanguageResultExecution timeMemory
765703marvinthangThe Big Prize (IOI17_prize)C++17
100 / 100
52 ms10864 KiB
/************************************* * author: marvinthang * * created: 25.06.2023 08:50:11 * *************************************/ #include "prize.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } // end of template mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class T> T rand(T l, T h) { return uniform_int_distribution <T> (l, h) (rng); } template <class T> T rand(T h) { return uniform_int_distribution <T> (0, h - 1) (rng); } int res; vector <set <int>> ss; vector <int> cnt_left; void solve(int l, int r) { if (~res || l > r) return; int m = l + r >> 1; vector <int> a = ask(m); cnt_left[m] = a[0]; int sum = a[0] + a[1]; if (!sum) { res = m; return; } auto it = ss[sum].insert(m).fi; if (it == ss[sum].begin()) { if (a[0]) solve(l, m - 1); } else { if (cnt_left[*prev(it)] < a[0]) solve(l, m - 1); } if (~res) return; if (++it != ss[sum].end()) { if (cnt_left[*it] > a[0]) solve(m + 1, r); } else { if (a[1]) solve(m + 1, r); } } int find_best(int n) { res = -1; ss.assign(n, set<int>()); cnt_left.assign(n, -1); solve(0, n - 1); return res; } #ifdef LOCAL #include "prize.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; static const int max_q = 5000; 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; } int main() { file("prize"); 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); if (g[res] != 1) return !(cout << "WA!\n"); cout << res << endl << "Query count: " << query_count << endl; return 0; } #endif

Compilation message (stderr)

prize.cpp: In function 'void solve(int, int)':
prize.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |  int m = l + r >> 1;
      |          ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...