Submission #1001008

#TimeUsernameProblemLanguageResultExecution timeMemory
1001008amine_arouaA Difficult(y) Choice (BOI21_books)C++17
60 / 100
2 ms2136 KiB
#include <bits/stdc++.h> using namespace std; #define intt long long #define forr(i , x , y) for(int i = x ;i <= y;i++) #define pb push_back #define fore(i , n) for(int i = 0 ; i <n;i++) /** * skims through the i-th book and returns its difficulty x_i (1 <= i <= N). */ long long skim(int); /** * buys your selection of books. This function has to be called with * v = {i_1, ..., i_K} (1 <= i_1, ..., i_K <= N) where the i_j's are pairwise * distinct and satisfy A <= x_{i_1} + ... + x_{i_K} <= 2 A. */ void answer(std::vector<int>); /** * states that it is impossible to buy a set of K books with the desired difficulty. */ void impossible(); intt get(int i , vector<intt> &x) { if(x[i] == -1) return x[i] = skim(i); return x[i]; } void solve(int n, int k, long long a, int s) { vector<intt> x(n + 1 , -1); vector<intt> pref(n + 1); vector<int> ans; forr(i , 1 , k - 1) { pref[i] = pref[i - 1] + get(i , x); } if(pref[k - 1] + get(k , x) > 2ll * a) { impossible(); } intt h = 0; for(int i = n ; i > n - k;i--) h+=get(i , x); if(h < a) { impossible(); } int l = 0 , r = n; while(l + 1 < r) { int mid = (l + r)/2; if(get(mid , x) >= a) { r = mid; } else l = mid; } if(get(r , x) >= a && r >= k && get(r , x) + pref[k - 1] <= 2ll*a) { forr(i , 1, k - 1) ans.pb(i); ans.pb(r); answer(ans); return ; } int lo = k, hi = r + 1; while(lo + 1 < hi) { int mid = (lo + hi)/2; intt sm= 0; forr(i , mid - k + 1 , mid) { sm+=get(i , x); if(sm > 2ll * a) { break; } } if(a <= sm && sm <= 2ll * a) { forr(i , mid - k + 1 , mid) ans.pb(i); answer(ans); return; } if(sm > 2ll * a) { hi = mid; } if(sm < a) lo = mid; } intt sm = 0; forr(i , lo - k +1 , lo) { sm+=get(i , x); } if(a <= sm && sm <= 2ll * a) { forr(i , lo - k +1 , lo) ans.pb(i); answer(ans); return ; } // sm = 0; // forr(i , hi - k +1 , hi) // { // sm+=get(i , x); // } // if(a <= sm && sm <= 2ll * a) // { // forr(i , hi - k +1 , hi) // ans.pb(i); // answer(ans); // return ; // } impossible(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...