Submission #897658

#TimeUsernameProblemLanguageResultExecution timeMemory
897658GrindMachineA Difficult(y) Choice (BOI21_books)C++17
100 / 100
1 ms600 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

read the sol a long time ago and remember some ideas from there

the answer can't contain anybody with val > 2A
assume the answer contains someone with A <= val <= 2A
then sum >= A is satisfied, we want to ensure that sum <= 2A
so we fill the rest of the k-1 spots with the a[1..k-1]
the smallest val >= A can be found by b.s

otherwise, the answer contains vals < A
if the sum of a[1..k] > 2A, then a solution does not exist (because this is the min possible sum that we can achieve)
if A <= sum of a[1..k] <= 2A, then [1..k] works
otherwise, sum of a[1..k] < A

key idea:
if curr_sum < A and we add some val < A, then sum <= 2A 

we will try to move from [1..k] to a solution
we have a pointer from behind, initially pointing to first_big-1 (we dont want to take anybody >= A, that is already covered in the previous case)
a[first_big-1] is the largest guy that is < A

we remove a[k] and add a[ptr] (ptr = first_big-1 initially)
sum increases by < A
if we have achieved the goal (A <= sum <= 2A), then ok
otherwise, decrease ptr by 1 and repeat the process for indices k-1,k-2,...,1

what to do if we can't find a solution until the end?
in this case, no such solution exists
because sum would be the sum of the k largest guys with val < A
if this sum is still < A, then a solution doesnt exist

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "books.h"

void solve(int n, int k, long long A, int S) {
    map<ll,ll> mp;

    auto my_skim = [&](ll i){
        if(mp.count(i)) return mp[i];
        else return mp[i] = skim(i);
    };

    ll first_big = n+1;
    ll l = 1, r = n;
    while(l <= r){
        ll mid = (l+r) >> 1;
        ll val = my_skim(mid);
        if(val >= A){
            first_big = mid;
            r = mid-1;
        }
        else{
            l = mid+1;
        }
    }

    vector<ll> a(k+5);
    rep1(i,k){
        a[i] = my_skim(i);
    }

    if(first_big <= n){
        ll sum = my_skim(first_big);
        rep1(i,k-1) sum += a[i];
        if(sum >= A and sum <= 2*A){
            vector<int> ans;
            rep1(i,k-1) ans.pb(i);
            ans.pb(first_big);
            answer(ans);
            return;
        }
    }

    ll sum = 0;
    rep1(i,k) sum += a[i];

    if(sum > 2*A){
        impossible();
        return;
    }

    deque<int> dq;
    rep1(i,k) dq.pb(i);

    if(sum >= A){
        vector<int> ans(all(dq));
        answer(ans);
        return;
    }

    ll ptr = first_big-1;

    rev(i,k,1){
        sum -= a[i];
        sum += my_skim(ptr);
        dq.pop_back();
        dq.push_front(ptr);
        ptr--;

        if(sum >= A){
            assert(sum <= 2*A);
            vector<int> ans(all(dq));
            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...