Submission #70396

# Submission time Handle Problem Language Result Execution time Memory
70396 2018-08-22T19:15:15 Z elitewantsyou Candies (JOI18_candies) C++14
100 / 100
179 ms 66072 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
 
using namespace std;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ldouble;
 
int N;
ll A[200020];
int nxt[200020], pre[200020], chk[200020];
 
int main() {
	scanf("%d", &N);
	for(int i=1;i<=N;i++) scanf("%lld", A+i);
	for(int i=1;i<=N+1;i++) pre[i] = i-1;
	for(int i=0;i<=N;i++) nxt[i] = i+1;
	nxt[N+1] = pre[0] = -1;
	A[0] = A[N+1] = -1e18;
	priority_queue <pll> pq; ll ans = 0;
	for(int i=1;i<=N;i++) pq.push(pll(A[i], i));
	for(int i=1;i<=(N+1)/2;i++) {
		pll t = pq.top(); pq.pop();
		if(chk[t.Se]) { --i; continue; }
		ans += t.Fi;
		int idx = (int)t.Se;
		int pr = pre[idx];
		int nx = nxt[idx];
		chk[pr] = chk[nx] = 1;
		A[idx] = -A[idx] + A[pr] + A[nx];
		pq.push(pll(A[idx], idx));
		nxt[idx] = nxt[nx];
		if(nxt[nx] != -1) pre[nxt[nx]] = idx;
		pre[idx] = pre[pr];
		if(pre[pr] != -1) nxt[pre[pr]] = idx;
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
candies.cpp:38:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=N;i++) scanf("%lld", A+i);
                        ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 612 KB Output is correct
3 Correct 4 ms 764 KB Output is correct
4 Correct 4 ms 784 KB Output is correct
5 Correct 4 ms 800 KB Output is correct
6 Correct 3 ms 820 KB Output is correct
7 Correct 4 ms 900 KB Output is correct
8 Correct 4 ms 924 KB Output is correct
9 Correct 3 ms 996 KB Output is correct
10 Correct 3 ms 1016 KB Output is correct
11 Correct 4 ms 1088 KB Output is correct
12 Correct 4 ms 1108 KB Output is correct
13 Correct 5 ms 1128 KB Output is correct
14 Correct 4 ms 1148 KB Output is correct
15 Correct 3 ms 1168 KB Output is correct
16 Correct 4 ms 1188 KB Output is correct
17 Correct 4 ms 1208 KB Output is correct
18 Correct 4 ms 1208 KB Output is correct
19 Correct 4 ms 1264 KB Output is correct
20 Correct 3 ms 1284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 612 KB Output is correct
3 Correct 4 ms 764 KB Output is correct
4 Correct 4 ms 784 KB Output is correct
5 Correct 4 ms 800 KB Output is correct
6 Correct 3 ms 820 KB Output is correct
7 Correct 4 ms 900 KB Output is correct
8 Correct 4 ms 924 KB Output is correct
9 Correct 3 ms 996 KB Output is correct
10 Correct 3 ms 1016 KB Output is correct
11 Correct 4 ms 1088 KB Output is correct
12 Correct 4 ms 1108 KB Output is correct
13 Correct 5 ms 1128 KB Output is correct
14 Correct 4 ms 1148 KB Output is correct
15 Correct 3 ms 1168 KB Output is correct
16 Correct 4 ms 1188 KB Output is correct
17 Correct 4 ms 1208 KB Output is correct
18 Correct 4 ms 1208 KB Output is correct
19 Correct 4 ms 1264 KB Output is correct
20 Correct 3 ms 1284 KB Output is correct
21 Correct 175 ms 11728 KB Output is correct
22 Correct 168 ms 13684 KB Output is correct
23 Correct 176 ms 15500 KB Output is correct
24 Correct 124 ms 17444 KB Output is correct
25 Correct 124 ms 19140 KB Output is correct
26 Correct 114 ms 21004 KB Output is correct
27 Correct 141 ms 22880 KB Output is correct
28 Correct 149 ms 24740 KB Output is correct
29 Correct 139 ms 26788 KB Output is correct
30 Correct 166 ms 28788 KB Output is correct
31 Correct 163 ms 30816 KB Output is correct
32 Correct 167 ms 32580 KB Output is correct
33 Correct 169 ms 34536 KB Output is correct
34 Correct 169 ms 36168 KB Output is correct
35 Correct 175 ms 37872 KB Output is correct
36 Correct 173 ms 39792 KB Output is correct
37 Correct 174 ms 41724 KB Output is correct
38 Correct 179 ms 43656 KB Output is correct
39 Correct 151 ms 45476 KB Output is correct
40 Correct 121 ms 47240 KB Output is correct
41 Correct 130 ms 49052 KB Output is correct
42 Correct 146 ms 51028 KB Output is correct
43 Correct 148 ms 52936 KB Output is correct
44 Correct 156 ms 54888 KB Output is correct
45 Correct 155 ms 56864 KB Output is correct
46 Correct 155 ms 58816 KB Output is correct
47 Correct 134 ms 60860 KB Output is correct
48 Correct 161 ms 62660 KB Output is correct
49 Correct 136 ms 64320 KB Output is correct
50 Correct 166 ms 66072 KB Output is correct