이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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();
}
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;
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 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... |