Submission #1361014

#TimeUsernameProblemLanguageResultExecution timeMemory
1361014AvianshTycho (BOI23_tycho)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e18;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int b,p,d,n;
    cin >> b >> p >> d >> n;
    int las = 0;
    multiset<array<int,2>>s;
    s.insert({0,0});
    int delta = 0;
    int laz = 0;
    while(n--){
        int a;
        cin >> a;
        ///merge las-a range into dp
        int len = a-las-1;
        int base = (len/p)*d;
        int tillpos = p-len%p;
        ///add base to starting point delta.
        if(base){
            laz+=base;
        }
        ///now make range tillpos-p in this shifted version
        ///(tillpos+delta)%p -> delta
        tillpos-=delta;
        tillpos%=p;
        tillpos+=p;
        tillpos%=p;
        int tillneg = p-delta;
        tillneg%=p;
        tillneg+=p;
        tillneg%=p;
        if(tillneg==0){
            auto it1 = s.lower_bound({tillpos,-inf});
            if(it1==s.end()){
                //do nothing
            }
            else{
                array<int,2>ol = (*it1);
                ol[1]+=d;
                s.erase(it1);
                if(ol[1]<0){
                    s.insert(ol);
                }
            }
        }
        else if(tillneg>tillpos){
            ///one single range
            auto it1 = s.lower_bound({tillpos,-inf});
            auto it2 = s.lower_bound({tillneg,-inf});
            if(it1==it2){
                //nothing happens
            }
            else{
                array<int,2>ol = (*it1);
                ol[1]+=d;
                s.erase(it1);
                if(ol[1]<0){
                    s.insert(ol);
                }
                s.insert({tillneg,-d});
            }
        }
        else if(tillneg<tillpos){
            ///prefix and suffix
            laz+=d;
            s.insert({tillneg,-d});
            auto it1 = s.lower_bound({tillpos,-inf});
            if(it1==s.end()){
                //do nothing
            }
            else{
                array<int,2>ol = (*it1);
                ol[1]+=d;
                s.erase(it1);
                if(ol[1]<0){
                    s.insert(ol);
                }
            }
        }
        else{
            laz+=d;
        }
        //shift
        delta += (len+1)%p;
        delta%=p;
        las=a;
    }

    int a=b;
    ///merge las-a range into dp
    int len = a-las-1;
    int base = (len/p)*d;
    int tillpos = p-len%p;
    ///add base to starting point delta.
    if(base){
        laz+=base;
    }
    ///now make range tillpos-p in this shifted version
    ///(tillpos+delta)%p -> delta
    tillpos-=delta;
    tillpos%=p;
    tillpos+=p;
    tillpos%=p;
    int tillneg = p-delta;
    tillneg%=p;
    tillneg+=p;
    tillneg%=p;
    if(tillneg==0){
        auto it1 = s.lower_bound({tillpos,-inf});
        if(it1==s.end()){
            //do nothing
        }
        else{
            array<int,2>ol = (*it1);
            ol[1]+=d;
            s.erase(it1);
            if(ol[1]<0){
                s.insert(ol);
            }
        }
    }
    else if(tillneg>tillpos){
        ///one single range
        auto it1 = s.lower_bound({tillpos,-inf});
        auto it2 = s.lower_bound({tillneg,-inf});
        if(it1==it2){
            //nothing happens
        }
        else{
            array<int,2>ol = (*it1);
            ol[1]+=d;
            s.erase(it1);
            if(ol[1]<0){
                s.insert(ol);
            }
            s.insert({tillneg,-d});
        }
    }
    else if(tillneg<tillpos){
        ///prefix and suffix
        laz+=d;
        s.insert({tillneg,-d});
        auto it1 = s.lower_bound({tillpos,-inf});
        if(it1==s.end()){
            //do nothing
        }
        else{
            array<int,2>ol = (*it1);
            ol[1]+=d;
            s.erase(it1);
            if(ol[1]<0){
                s.insert(ol);
            }
        }
    }
    else{
        laz+=d;
    }
    //shift
    delta += (len+1)%p;
    delta%=p;
    las=a;
    int curr = laz;
    int ans = inf;
    las = 0;
    for(array<int,2>a:s){
        if(las!=a[0])
            ans=min(ans,curr);
        curr+=a[1];
        curr+=(a[0]-las);
        las=a[0];
    }
    ans=min(ans,curr);
    ans+=b;
    cout << ans;
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...