# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
338224 | Kerim | Mountain Trek Route (IZhO12_route) | C++17 | 1 ms | 512 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#define MAXN 1000009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN],l[MAXN],r[MAXN],ata[MAXN],sz[MAXN];
int tap(int x){
if(ata[x]==x)return x;
return ata[x]=tap(ata[x]);
}
void merge(int x,int y){
if((x=tap(x))==(y=tap(y)))
return;
ata[x]=y;l[y]=l[x];sz[y]++;
}
int main(){
//freopen("file.in", "r", stdin);
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",arr+i);
for(int i=1;i<=n;i++){
ata[i]=i,sz[i]=1;
if(i>1) l[i]=i-1;
else l[i]=n;
if(i<n) r[i]=i+1;
else r[i]=1;
}
for(int i=1;i<=n;i++)
if(arr[i]==arr[r[i]])
merge(i,r[i]);
priority_queue<PII>q;
for(int i=1;i<=n;i++)
if(ata[i]==i and arr[i]<min(arr[l[i]],arr[r[i]]))
q.push(mp(sz[i],i));
ll ans=0;
while(!q.empty()){
int nd=q.top().ss;q.pop();
int val=min(arr[tap(l[nd])],arr[tap(r[nd])])-arr[nd];
if(val*1LL*sz[nd]>=k){
ans+=2LL*(k/sz[nd]);
break;
}
ans+=2LL*val;
k-=sz[nd]*val;
arr[nd]+=val;
if(arr[nd]==arr[tap(l[nd])]) merge(l[nd],nd);
if(arr[nd]==arr[tap(r[nd])]) merge(nd,r[nd]);
nd=tap(nd);
if(arr[nd]<min(arr[tap(l[nd])],arr[tap(r[nd])]))
q.push(mp(sz[nd],nd));
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |