제출 #814826

#제출 시각아이디문제언어결과실행 시간메모리
814826winter0101A Difficult(y) Choice (BOI21_books)C++14
100 / 100
1 ms336 KiB
#include<bits/stdc++.h> #include "books.h" using namespace std; #define all(fl) fl.begin(),fl.end() #define pb push_back #define fi first #define se second #define for1(i,j,k) for(int i=j;i<=k;i++) #define for2(i,j,k) for(int i=j;i>=k;i--) #define for3(i,j,k,l) for(int i=j;i<=k;i+=l) #define lb lower_bound #define ub upper_bound #define sz(a) (int)a.size() #define pii pair<int,int> #define pli pair<long long,int> #define gcd __gcd #define lcm(x,y) x*y/__gcd(x,y) /*typedef long long ll; void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...) { va_list args; va_start(args, msg); vfprintf(stderr, msg, args); fprintf(stderr, "\n"); va_end(args); exit(0); } namespace { int N, K, S, sUsed; long long A; vector<long long> seq; } void impossible() { result("Impossible (not checked): %d book(s) skimmed", sUsed); exit(0); } long long skim(int pos) { printf("skim(%d): ", pos); if (pos<1 || pos>N) result("Invalid skim"); printf("%lld\n", seq[pos]); sUsed++; if (sUsed>S) result("Out of books to skim"); return seq[pos]; } void answer(vector<int> v) { printf("answer({"); for(int i = 0; i < (int) v.size(); ++i) { printf("%d", v[i]); if(i + 1 != (int) v.size()) printf(", "); } printf("})\n"); if ((int) v.size() != K) result("Invalid answer"); ll sum = 0; for(auto x: v) { if (x<1 || x>N) result("Invalid answer"); sum += seq[x]; } if (sum < A || 2*A<sum) result("Wrong answer"); result("Correct: %d book(s) skimmed", sUsed); exit(0); }*/ const int maxn=1e5+9; long long a[maxn]; long long b[maxn]; long long ask(int id){ if (a[id])return a[id]; return a[id]=skim(id); } void solve(int n,int k,long long A,int s){ for1(i,1,k-1){ a[i]=ask(i); b[i]=b[i-1]+a[i]; } vector<int>id; long long sum=0; for1(i,1,k){ int l=k-i+1,r=n,c1=-1; if (!id.empty())r=id.back()-1; long long v1=ask(r); if (v1+sum+b[k-i]<=2*A){ c1=r; id.pb(c1); sum+=a[c1]; if (sum+b[k-i]>=A){ for1(j,1,k-i){ id.pb(j); } answer(id); return; } continue; } while (l<=r){ int mid=(l+r)/2; long long vl=ask(mid); if (vl+sum+b[k-i]<=2*A){ l=mid+1; c1=mid; } else r=mid-1; } //cerr<<c1<<'\n'; if (c1==-1)break; id.pb(c1); sum+=a[c1]; if (sum+b[k-i]>=A){ for1(j,1,k-i){ id.pb(j); } answer(id); return; } //cerr<<c1<<" "<<sum<<'\n'; } if (sz(id)!=k||sum<A){ impossible(); } else { answer(id); } } /*signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen(".INP","r",stdin); //freopen(".OUT","w",stdout); if(scanf("%d %d %lld %d", &N, &K, &A, &S) != 4) result("Invalid input"); seq.resize(N + 1); for(int i = 1; i <= N; ++i) { if(scanf("%lld", &(seq[i])) != 1) result("Invalid input"); if(i>1 && seq[i]<=seq[i-1]) result("Invalid input"); } solve(N, K, A, S); result("No answer"); }*/
#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...