# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
639524 | Benmath | A Difficult(y) Choice (BOI21_books) | C++14 | 3 ms | 984 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
//
// --- Sample implementation for the task books ---
//
// To compile this program with the sample grader, place:
// books.h books_sample.cpp sample_grader.cpp
// in a single folder and run:
// g++ books_sample.cpp sample_grader.cpp
// in this folder.
//
void solve(int N, int K, long long A, int S) {
// TODO implement this function
long long int so[N+1];
for(int i=0;i<=N;i++){
so[i]=-1;
}
int l=1;
int r=N;
int t1=N+1;
int brojac=0;
while(l<=r){
int mid=l+(r-l)/2;
so[mid]=skim(mid);
brojac++;
if(so[mid]>A){
t1=min(t1,mid);
r=mid-1;
}else{
l=mid+1;
}
}
long long int sum1=0;
long long int sum=0;
for(int i=1;i<=K;i++){
if(so[i]==-1){
so[i]=skim(i);
}
sum1=sum1+so[i];
}
if(sum1>2*A){
impossible();
return;
}else if(sum1>=A){
vector<int>v;
for(int i=1;i<=K;i++){
v.push_back(i);
}
answer(v);
return;
}else{
if(t1==(N+1)){
sum=0;
vector<int>v;
for(int i=N;i>(N-K);i--){
if(so[i]==-1){
so[i]=skim(i);
}
sum=sum+so[i];
v.push_back(i);
}
if(sum<A){
impossible();
}else if(sum<=2*A){
answer(v);
return;
}else{
vector<int>a;
if(N<=20){
for(int i=1;i<=N;i++){
a.push_back(i);
}
}else{
for(int i=1;i<=K;i++){
a.push_back(i);
}
for(int i=(N-K+1);i<=N;i++){
a.push_back(i);
}
}
long long int sum2=0;
for(int i=1;i<=K;i++){
sum2=sum2+so[i];
}
vector<int>v1;
for(int i=K;i<a.size();i++){
int j1=a[i];
int j2=a[i-K];
sum2=sum2+so[j1]-so[j2];
if(sum2>=A and sum2<=2*A){
for(int k=i-K+1;k<=i;k++){
v1.push_back(a[k]);
}
answer(v1);
return;
}
}
}
}else{
vector<int>v;
sum=0;
for(int i=1;i<K;i++){
sum=sum+so[i];
v.push_back(i);
}
sum=sum+so[t1];
v.push_back(t1);
if(sum>=A and sum<=2*A){
answer(v);
return;
}
vector<int>v1;
sum=0;
for(int i=t1-K+1;i<=t1;i++){
if(so[i]==-1){
so[i]=skim(i);
}
sum=sum+so[i];
v1.push_back(i);
}
if(sum>=A and sum<=2*A){
answer(v1);
return;
}
vector<int>v2;
sum=0;
if(t1>K){
for(int i=t1-K;i<=(t1-1);i++){
if(so[i]==-1){
so[i]=skim(i);
}
sum=sum+so[i];
v2.push_back(i);
}
if(sum>=A and sum<=2*A){
answer(v2);
return;
}
}
}
}
impossible();
/*
if(t1==(N+1)){
impossible();
return;
}else{
long long int sum=0;
vector<int>v;
for(int i=1;i<K;i++){
v.push_back(i);
long long int r=so[i];
if(r==-1){
r=skim(i);
so[i]=r;
}
sum=sum+r;
}
v.push_back(t1+K-1);
long long int r1=so[t1+K-1];
if(r1==-1){
r1=skim(t1+K-1);
}
sum=sum+r1;
if(sum<=2*A){
answer(v);
}else{
impossible();
}
}
/*
int n=N;
long long int arr[n];
for(int i=0;i<n;i++){
arr[i]=skim(i+1);
}
long long int sum=0;
for(int i=0;i<K;i++){
sum=sum+arr[i];
}
if(sum>=A and sum<=2*A){
vector<int>v;
for(int i=0;i<K;i++){
v.push_back(i+1);
}
answer(v);
return;
}
for(int i=K;i<N;i++){
sum=sum+arr[i]-arr[i-K];
if(sum>=A and sum<=2*A){
vector<int>v;
for(int j=i-K+1;j<=i;j++){
v.push_back(j+1);
}
answer(v);
return;
}
if(sum>2*A){
sum=0;
vector<int>v;
for(int j=0;j<(K-1);j++){
sum=sum+arr[j];
v.push_back(j+1);
}
v.push_back(i+1);
sum=sum+arr[i];
if(sum>=A and sum<=2*A){
answer(v);
return ;
}else{
impossible();
return;
}
}
}
impossible();
*/
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |