Submission #3783864


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int n,m;
    cin >> n >> m;
    int a[n];
    vector<pair<int,int>> b(n);
    cin >> a[0];
    b[0]=make_pair(a[0],0);
    int k=0;
    int w=0;
    int ans=0;
    for(int i=1;i<n;i++){
        cin >>a[i];
        
        b[i]=make_pair(a[i],i);
        
        if(a[i-1]>a[i]){
            w++;
            k+= m-a[i-1]-1;
            ans+=m+a[i]-a[i-1];

        }else{
            ans+=a[i]-a[i-1];
        }

    }
    sort(b.begin(),b.end());
    int t;
    int gmax =0;
    int g=0;
    for(int i=0;i<n;i++){
        g=k+w*b[i].first;
        if(i==0||((b[i-1].first!=b[i].first)&&(gmax<g)))gmax=g;
        switch(b[i].second){
            case 0:
                k+=-a[0]-1;
                w++;
                break;
           
            default:
                if(b[i].second==n-1){
                    t=a[n-2]-a[n-1];
                    if(t>0)t-=m;
                    k+=t+a[n-1]+1;
                    w--;
                }else{
                    t=a[b[i].second -1]-b[i].first;
                    if(t>0)t-=m;
                    k+=t;
                }
                
                break;
        }
    }
    cout <<ans-gmax;
    return 0;
}

Submission Info

Submission Time
Task E - guruguru
User yatuba
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1318 Byte
Status AC
Exec Time 38 ms
Memory 2560 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample1.txt, sample2.txt
All half0.txt, half1.txt, half2.txt, half3.txt, half4.txt, min_m.txt, min_n.txt, mx0.txt, mx1.txt, mx2.txt, rnd0.txt, rnd1.txt, rnd2.txt, rnd3.txt, rnd4.txt, rnd5.txt, rnd6.txt, rnd7.txt, rnd8.txt, rnd9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
half0.txt AC 10 ms 768 KB
half1.txt AC 4 ms 384 KB
half2.txt AC 17 ms 1280 KB
half3.txt AC 27 ms 1920 KB
half4.txt AC 29 ms 1920 KB
min_m.txt AC 1 ms 256 KB
min_n.txt AC 1 ms 256 KB
mx0.txt AC 38 ms 2560 KB
mx1.txt AC 37 ms 2560 KB
mx2.txt AC 38 ms 2560 KB
rnd0.txt AC 1 ms 256 KB
rnd1.txt AC 1 ms 256 KB
rnd2.txt AC 3 ms 384 KB
rnd3.txt AC 7 ms 640 KB
rnd4.txt AC 26 ms 2048 KB
rnd5.txt AC 2 ms 384 KB
rnd6.txt AC 1 ms 256 KB
rnd7.txt AC 2 ms 256 KB
rnd8.txt AC 4 ms 384 KB
rnd9.txt AC 1 ms 256 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB