Submission #67785941


Source Code Expand

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int p=998244353;
int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}}
int inv(int x) {return po(x,p-2);}
mt19937 rnd;
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#define debugv(v) do {cout<< #v <<" : {"; for(int izxc=0;izxc<v.size();++izxc) {cout << v[izxc];if(izxc+1!=v.size()) cout << ","; }cout <<"}"<< endl;} while(0)
#else
#define debug(...)
#define debugv(v)
#endif
#define lob(a,x) lower_bound(all(a),x)
#define upb(a,x) upper_bound(all(a),x)
#define print(x) cout<<(x)<<'\n'
#define suma(a) accumulate(all(a),0LL)
#define maxpos(a) max_element(all(a))-a.begin()
#define minpos(a) min_element(all(a))-a.begin()
#define maxa(a) *max_element(all(a))
#define mina(a) *min_element(all(a))
pair<int,vector<int> > read()
{
    int n;cin>>n;vector<int> a(n);for(int &x:a) {cin>>x;} return {n,a};
}
int stupid(vector<int> a) {
    int n=a.size();
    if(a.size()==1) return 0;
    int pos=minpos(a);
    int res=0;
    if(pos==0) {
        res=a[1]-a[0];
        a.erase(a.begin());a[0]++;
    }
    else if(pos==n-1) {
        res=a[n-2]-a[n-1];
        a.pop_back();a[n-2]++;
    }
    else if(a[pos-1]<a[pos+1]) {
        res=a[pos-1]-a[pos];
        a[pos-1]++;a.erase(a.begin()+pos);
    }
    else {
        res=a[pos+1]-a[pos];
        a[pos+1]++;a.erase(a.begin()+pos);
    }
    return res+stupid(a);
}
int solve(vector<int> a) {
    int n=a.size();
    a.insert(a.begin(),1e18);a.app(1e18);n+=2;
        vector<int> nx(n);for(int i=0;i<n;++i) nx[i]=i+1;
        vector<int> pr(n);for(int i=0;i<n;++i) pr[i]=i-1;
        set<pair<int,int> > poses;for(int i=1;i<n-1;++i) poses.insert({a[i],i});
        int ans=0;
        while(poses.size()>1) {
            auto [val,i]=(*poses.begin());
            int il=pr[i];int ir=nx[i];
            int v=min(a[il],a[ir]);
            if(val<v) {
                poses.erase(poses.begin());
                ans+=(v-val);
                a[i]=v;
                poses.insert({a[i],i});
                continue;
            }
            nx[il]=ir;pr[ir]=il;
            int ii;
            if(il==0) {ii=ir;}
            else if(ir==n-1) {ii=il;}
            else if(a[il]<=a[ir]) {ii=il;}
            else {ii=ir;}
            poses.erase({a[i],i});poses.erase({a[ii],ii});
            ans+=(a[ii]-a[i]);
            a[ii]++;
            poses.insert({a[ii],ii});
        }
        return ans;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    /*while(true) {
        vector<int> a(5);for(int &x:a) {x=rnd()%10+1;}
        int so=solve(a);int stu=stupid(a);
        if(so>stu) {
            debugv(a);
            debug(so,stu);
            exit(0);
        }
    }*/
    int t;cin>>t;
    while(t--) {
        int n;cin>>n;vector<int> a(n);for(int &x:a) {cin>>x;}
        cout<<solve(a)<<'\n';
    }
    return 0;
}

Submission Info

Submission Time
Task A - Merge and Increment
User turmax
Language C++ 20 (gcc 12.2)
Score 700
Code Size 3205 Byte
Status AC
Exec Time 266 ms
Memory 22008 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 64
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 03_sorted_00.txt, 03_sorted_01.txt, 03_sorted_02.txt, 03_sorted_03.txt, 03_sorted_04.txt, 03_sorted_05.txt, 04_almost_sorted_00.txt, 04_almost_sorted_01.txt, 04_almost_sorted_02.txt, 04_almost_sorted_03.txt, 04_almost_sorted_04.txt, 04_almost_sorted_05.txt, 05_same_00.txt, 05_same_01.txt, 05_same_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 07_hack_1_00.txt, 07_hack_1_01.txt, 07_hack_1_02.txt, 07_hack_1_03.txt, 07_hack_1_04.txt, 08_hack_2_00.txt, 08_hack_2_01.txt, 08_hack_2_02.txt, 08_hack_2_03.txt, 08_hack_2_04.txt, 09_hack_3_00.txt, 09_hack_3_01.txt, 09_hack_3_02.txt, 09_hack_3_03.txt, 09_hack_3_04.txt, 09_hack_3_05.txt, 09_hack_3_06.txt, 09_hack_3_07.txt, 09_hack_3_08.txt, 09_hack_3_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3520 KiB
01_small_00.txt AC 34 ms 3476 KiB
01_small_01.txt AC 33 ms 3532 KiB
01_small_02.txt AC 24 ms 3404 KiB
01_small_03.txt AC 40 ms 3488 KiB
01_small_04.txt AC 40 ms 3468 KiB
01_small_05.txt AC 41 ms 3536 KiB
01_small_06.txt AC 42 ms 3388 KiB
01_small_07.txt AC 49 ms 3452 KiB
01_small_08.txt AC 57 ms 3404 KiB
01_small_09.txt AC 31 ms 3444 KiB
01_small_10.txt AC 32 ms 3528 KiB
01_small_11.txt AC 32 ms 3332 KiB
01_small_12.txt AC 32 ms 3388 KiB
01_small_13.txt AC 45 ms 3532 KiB
01_small_14.txt AC 59 ms 3480 KiB
02_random_00.txt AC 183 ms 19428 KiB
02_random_01.txt AC 232 ms 21864 KiB
02_random_02.txt AC 192 ms 19088 KiB
02_random_03.txt AC 209 ms 21844 KiB
02_random_04.txt AC 232 ms 21220 KiB
02_random_05.txt AC 245 ms 21756 KiB
02_random_06.txt AC 126 ms 14600 KiB
02_random_07.txt AC 244 ms 21992 KiB
02_random_08.txt AC 123 ms 14588 KiB
02_random_09.txt AC 238 ms 21944 KiB
03_sorted_00.txt AC 96 ms 21840 KiB
03_sorted_01.txt AC 104 ms 21860 KiB
03_sorted_02.txt AC 103 ms 21856 KiB
03_sorted_03.txt AC 96 ms 21924 KiB
03_sorted_04.txt AC 104 ms 21920 KiB
03_sorted_05.txt AC 104 ms 21860 KiB
04_almost_sorted_00.txt AC 97 ms 21832 KiB
04_almost_sorted_01.txt AC 105 ms 21948 KiB
04_almost_sorted_02.txt AC 104 ms 21832 KiB
04_almost_sorted_03.txt AC 97 ms 21852 KiB
04_almost_sorted_04.txt AC 104 ms 21928 KiB
04_almost_sorted_05.txt AC 104 ms 21932 KiB
05_same_00.txt AC 120 ms 22008 KiB
05_same_01.txt AC 125 ms 21920 KiB
05_same_02.txt AC 125 ms 21928 KiB
06_corner_00.txt AC 134 ms 21840 KiB
06_corner_01.txt AC 104 ms 21896 KiB
06_corner_02.txt AC 99 ms 21880 KiB
07_hack_1_00.txt AC 108 ms 21896 KiB
07_hack_1_01.txt AC 119 ms 21832 KiB
07_hack_1_02.txt AC 117 ms 21912 KiB
07_hack_1_03.txt AC 105 ms 21884 KiB
07_hack_1_04.txt AC 130 ms 21924 KiB
08_hack_2_00.txt AC 105 ms 21948 KiB
08_hack_2_01.txt AC 104 ms 21860 KiB
08_hack_2_02.txt AC 106 ms 22000 KiB
08_hack_2_03.txt AC 103 ms 22000 KiB
08_hack_2_04.txt AC 104 ms 22000 KiB
09_hack_3_00.txt AC 120 ms 21840 KiB
09_hack_3_01.txt AC 119 ms 21836 KiB
09_hack_3_02.txt AC 122 ms 21828 KiB
09_hack_3_03.txt AC 122 ms 21840 KiB
09_hack_3_04.txt AC 219 ms 21852 KiB
09_hack_3_05.txt AC 210 ms 21940 KiB
09_hack_3_06.txt AC 266 ms 21888 KiB
09_hack_3_07.txt AC 245 ms 21988 KiB
09_hack_3_08.txt AC 257 ms 21928 KiB
09_hack_3_09.txt AC 248 ms 21992 KiB