답안 #338229

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338229 2020-12-22T19:06:02 Z Kerim 등산 경로 (IZhO12_route) C++17
100 / 100
188 ms 29420 KB
#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]+=sz[x];	
}
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;
		assert(-q.top().ff==sz[nd]);
		q.pop();
		int val=min(arr[tap(l[nd])],arr[tap(r[nd])])-arr[nd];
		if(val*1LL*sz[nd]>=k){
			ans+=2*(k/sz[nd]);
			break;
		}
		ans+=2*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

route.cpp: In function 'int main()':
route.cpp:47:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   47 |     for(int i=1;i<=n;i++)
      |     ^~~
route.cpp:50:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   50 |  ll ans=0;
      |  ^~
route.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
route.cpp:35:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   35 |      scanf("%d",arr+i);
      |      ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 14 ms 3308 KB Output is correct
11 Correct 17 ms 3944 KB Output is correct
12 Correct 16 ms 4716 KB Output is correct
13 Correct 150 ms 29420 KB Output is correct
14 Correct 155 ms 29420 KB Output is correct
15 Correct 131 ms 27500 KB Output is correct
16 Correct 143 ms 28524 KB Output is correct
17 Correct 137 ms 28524 KB Output is correct
18 Correct 145 ms 29292 KB Output is correct
19 Correct 188 ms 29420 KB Output is correct
20 Correct 148 ms 25732 KB Output is correct