답안 #245437

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
245437 2020-07-06T12:07:16 Z arnold518 Candies (JOI18_candies) C++14
100 / 100
943 ms 36856 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;
const ll INF = 1e16;

int N;
ll A[MAXN+10], ans;

set<int> L, R;
set<pll, greater<pll>> S;

pii f(int x)
{
	int l, r;
	r=*R.lower_bound(x);
	l=*(--L.upper_bound(x));
	return {l, r};
}

int main()
{
    int i, j;

    scanf("%d", &N);
    for(i=1; i<=N; i++) scanf("%lld", &A[i]);
    A[0]=A[N+1]=-INF;
    for(i=0; i<=N+1; i++) L.insert(i), R.insert(i);
	for(i=0; i<=N+1; i++) S.insert({A[i], i});

	for(i=1; i<=(N+1)/2; i++)
	{
		ans+=S.begin()->first;
		printf("%lld\n", ans);
		int x=S.begin()->second;

		int l=f(x).first, r=f(x).second, pl=f(l-1).first, pr=f(l-1).second, ql=f(r+1).first, qr=f(r+1).second;

		L.erase(l), R.erase(r), S.erase({A[l], l});
		L.erase(pl), R.erase(pr), S.erase({A[pl], pl});
		L.erase(ql), R.erase(qr), S.erase({A[ql], ql});

		A[pl]=A[pl]+A[ql]-A[l];
		L.insert(pl); R.insert(qr); S.insert({A[pl], pl});			
	}
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:27:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
candies.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
candies.cpp:30:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) scanf("%lld", &A[i]);
                         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 640 KB Output is correct
2 Correct 8 ms 640 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 8 ms 768 KB Output is correct
5 Correct 8 ms 640 KB Output is correct
6 Correct 8 ms 640 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 8 ms 640 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 7 ms 640 KB Output is correct
11 Correct 8 ms 640 KB Output is correct
12 Correct 8 ms 640 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 8 ms 640 KB Output is correct
15 Correct 8 ms 640 KB Output is correct
16 Correct 8 ms 768 KB Output is correct
17 Correct 8 ms 640 KB Output is correct
18 Correct 8 ms 640 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 7 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 640 KB Output is correct
2 Correct 8 ms 640 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 8 ms 768 KB Output is correct
5 Correct 8 ms 640 KB Output is correct
6 Correct 8 ms 640 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 8 ms 640 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 7 ms 640 KB Output is correct
11 Correct 8 ms 640 KB Output is correct
12 Correct 8 ms 640 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 8 ms 640 KB Output is correct
15 Correct 8 ms 640 KB Output is correct
16 Correct 8 ms 768 KB Output is correct
17 Correct 8 ms 640 KB Output is correct
18 Correct 8 ms 640 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 7 ms 640 KB Output is correct
21 Correct 901 ms 36736 KB Output is correct
22 Correct 906 ms 36728 KB Output is correct
23 Correct 943 ms 36696 KB Output is correct
24 Correct 450 ms 36472 KB Output is correct
25 Correct 454 ms 36524 KB Output is correct
26 Correct 483 ms 36440 KB Output is correct
27 Correct 470 ms 36648 KB Output is correct
28 Correct 478 ms 36616 KB Output is correct
29 Correct 490 ms 36600 KB Output is correct
30 Correct 491 ms 36644 KB Output is correct
31 Correct 518 ms 36728 KB Output is correct
32 Correct 516 ms 36600 KB Output is correct
33 Correct 656 ms 36532 KB Output is correct
34 Correct 666 ms 36476 KB Output is correct
35 Correct 666 ms 36588 KB Output is correct
36 Correct 938 ms 36624 KB Output is correct
37 Correct 899 ms 36740 KB Output is correct
38 Correct 941 ms 36728 KB Output is correct
39 Correct 488 ms 36600 KB Output is correct
40 Correct 438 ms 36600 KB Output is correct
41 Correct 457 ms 36600 KB Output is correct
42 Correct 469 ms 36576 KB Output is correct
43 Correct 464 ms 36816 KB Output is correct
44 Correct 490 ms 36856 KB Output is correct
45 Correct 506 ms 36728 KB Output is correct
46 Correct 518 ms 36728 KB Output is correct
47 Correct 486 ms 36728 KB Output is correct
48 Correct 682 ms 36472 KB Output is correct
49 Correct 718 ms 36472 KB Output is correct
50 Correct 713 ms 36468 KB Output is correct