답안 #198553

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198553 2020-01-26T14:37:53 Z gs18081 Candies (JOI18_candies) C++11
100 / 100
396 ms 25976 KB
#include <bits/stdc++.h>

using namespace std;
	
typedef long long int ll;
typedef pair<int,int> pii;
typedef tuple<ll,int,int,int> ti;

const int MAXN = 202020;

struct UF{
	int n;
	vector<int> prt;
	vector<int> s;
	vector<int> e;
	UF(){}
	void resize(int size){
		n = size;
		prt = vector<int> (n+1,0);
		s = vector<int> (n+1,0);
		e = vector<int> (n+1,0);
		for(int i=1;i<=n;i++){
			s[i] = e[i] = prt[i] = i;
		}
	}
	int findprt(int node){
		if(prt[node] == node) return node;
		return prt[node] = findprt(prt[node]);
	}
	void merge(int a,int b){
		a = findprt(a); b = findprt(b);
		s[a] = min(s[a],s[b]);
		e[a] = max(e[a],e[b]);
		prt[b] = a;
	}
	pii getrange(int num){
		num = findprt(num);
		return pii(s[num],e[num]);
	}
}uf;

int N;
ll arr[MAXN];
ll sum[MAXN];
ll hsum[MAXN];
ll zsum[MAXN];
int vis[MAXN];
set<ti> st;
ll now = 0;

int main(){
	scanf("%d",&N);
	uf.resize(N);
	for(int i=1;i<=N;i++){
		scanf("%lld",&arr[i]);
	}
	for(int i=1;i<=N;i++){
		sum[i] = arr[i] + sum[i-1];
		hsum[i] = hsum[i-1];
		zsum[i] = zsum[i-1];
		if(i & 1) hsum[i] += arr[i];
		else zsum[i] += arr[i];
		st.insert(ti(arr[i],i,i,i));
	}
	while(!st.empty()){
		auto it = st.end();
		it--;
		ll c;
		int num;
		int s,e;
		tie(c,num,s,e) = *it;
		st.erase(it);
		int ts,te;
		tie(ts,te) = uf.getrange(num);
		if(s != ts || e != te) continue;
		if(vis[s] || vis[e]) continue;
		vis[s] = 1;
		vis[e] = 1;
		now += c;
		int flag = 0;
		if(s != 1){
			uf.merge(num,s - 1);
			flag = 1;
		}
		if(e != N) {
			uf.merge(num,e + 1);
			flag = 1;
		}
		tie(s,e) = uf.getrange(num);
		if(flag && (s + e) % 2 == 0){
			ll sum = 0;
			if(s & 1) {
				sum = hsum[e] - hsum[s-1] - (zsum[e-1] - zsum[s]);
			}
			else {
				sum = zsum[e] - zsum[s-1] - (hsum[e-1] - hsum[s]);
			}
			st.insert(ti(sum,s,s,e));
		}
		printf("%lld\n",now);
	}
	return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
  ~~~~~^~~~~~~~~
candies.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&arr[i]);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 632 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 7 ms 632 KB Output is correct
5 Correct 7 ms 632 KB Output is correct
6 Correct 7 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 6 ms 632 KB Output is correct
9 Correct 7 ms 632 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 7 ms 504 KB Output is correct
12 Correct 6 ms 632 KB Output is correct
13 Correct 7 ms 632 KB Output is correct
14 Correct 7 ms 632 KB Output is correct
15 Correct 7 ms 632 KB Output is correct
16 Correct 7 ms 632 KB Output is correct
17 Correct 7 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 7 ms 632 KB Output is correct
20 Correct 6 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 632 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 7 ms 632 KB Output is correct
5 Correct 7 ms 632 KB Output is correct
6 Correct 7 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 6 ms 632 KB Output is correct
9 Correct 7 ms 632 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 7 ms 504 KB Output is correct
12 Correct 6 ms 632 KB Output is correct
13 Correct 7 ms 632 KB Output is correct
14 Correct 7 ms 632 KB Output is correct
15 Correct 7 ms 632 KB Output is correct
16 Correct 7 ms 632 KB Output is correct
17 Correct 7 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 7 ms 632 KB Output is correct
20 Correct 6 ms 632 KB Output is correct
21 Correct 378 ms 25720 KB Output is correct
22 Correct 389 ms 25720 KB Output is correct
23 Correct 386 ms 25816 KB Output is correct
24 Correct 195 ms 25592 KB Output is correct
25 Correct 170 ms 25592 KB Output is correct
26 Correct 191 ms 25592 KB Output is correct
27 Correct 184 ms 25732 KB Output is correct
28 Correct 183 ms 25720 KB Output is correct
29 Correct 188 ms 25848 KB Output is correct
30 Correct 185 ms 25720 KB Output is correct
31 Correct 192 ms 25848 KB Output is correct
32 Correct 193 ms 25836 KB Output is correct
33 Correct 263 ms 25592 KB Output is correct
34 Correct 256 ms 25592 KB Output is correct
35 Correct 260 ms 25688 KB Output is correct
36 Correct 381 ms 25720 KB Output is correct
37 Correct 396 ms 25720 KB Output is correct
38 Correct 376 ms 25848 KB Output is correct
39 Correct 169 ms 25592 KB Output is correct
40 Correct 169 ms 25592 KB Output is correct
41 Correct 176 ms 25592 KB Output is correct
42 Correct 184 ms 25848 KB Output is correct
43 Correct 185 ms 25720 KB Output is correct
44 Correct 189 ms 25976 KB Output is correct
45 Correct 190 ms 25720 KB Output is correct
46 Correct 188 ms 25720 KB Output is correct
47 Correct 187 ms 25720 KB Output is correct
48 Correct 272 ms 25464 KB Output is correct
49 Correct 286 ms 25464 KB Output is correct
50 Correct 265 ms 25464 KB Output is correct