답안 #814708

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
814708 2023-08-08T09:10:17 Z BamshooT Feast (NOI19_feast) C++14
100 / 100
167 ms 17388 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long,int> ii;
const int N = 300005;
long long n, k, m = 1, a[N], L[N], R[N], cntD = 0;
set<ii> sf, sd;

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >>n >>k;
	for(int i=1; i<=n; i++)
		cin >>a[i];
	m = 0;
	for(int i=1; i<=n; i++)
		if(a[i] != 0) a[++m] = a[i];
	n = m, m = 1;
	while (m <= n && a[m] < 0) m++;
	int u = 1;
	a[1] = a[m];
	for(int i=m+1; i<=n; i++)
		if(a[i]*a[i-1] > 0)
			a[u] += a[i];
		else
			a[++u] = a[i];
	if(a[u] < 0) u--;
	m = u;
	for(int i=1; i<=m; i++)
	{
		cntD += (a[i] > 0);
		sf.insert({abs(a[i]), i});
		L[i] = i-1, R[i] = i+1;
	}
	while(cntD > k)
	{
		int v = sf.begin()->first, i = sf.begin()->second;
		int x = L[i], y = R[i];
		sf.erase(sf.begin());
		if(L[i]==0 && a[i] <= 0)
			L[R[i]] = 0;
		else if(R[i]>m && a[i] <= 0)
			R[L[i]] = m+1;
		else if(L[i] >= 1 && R[i] <= m)
		{
			cntD--;
			sf.erase(sf.find({abs(a[x]), x}));
			sf.erase(sf.find({abs(a[y]), y}));
			a[x] = a[x] + a[i] + a[y];
			sf.insert({abs(a[x]), x});
			L[R[y]] = x, R[x] = R[y];
		}
		else if(L[i] >= 1)
		{
			cntD--;
			sf.erase(sf.find({abs(a[x]), x}));
			a[x] = a[x] + a[i];
			sf.insert({abs(a[x]), x});
			R[x] = m+1;
		}
		else if(R[i] <= m)
		{
			cntD--;
			sf.erase(sf.find({abs(a[y]), y}));
			a[i] = a[i] + a[y];
			sf.insert({abs(a[i]), i});
			L[R[y]] = i, R[i] = R[y];
		}
	}
	long long sum = 0;
	for(auto x : sf)
	{
		if(a[x.second] > 0)
			sum += x.first;
	}
	cout <<sum;
	return 0;
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:36:7: warning: unused variable 'v' [-Wunused-variable]
   36 |   int v = sf.begin()->first, i = sf.begin()->second;
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 5308 KB Output is correct
2 Correct 25 ms 5440 KB Output is correct
3 Correct 36 ms 5488 KB Output is correct
4 Correct 24 ms 5456 KB Output is correct
5 Correct 30 ms 5420 KB Output is correct
6 Correct 25 ms 5324 KB Output is correct
7 Correct 27 ms 5348 KB Output is correct
8 Correct 30 ms 5380 KB Output is correct
9 Correct 24 ms 5384 KB Output is correct
10 Correct 24 ms 5344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 3660 KB Output is correct
2 Correct 16 ms 3764 KB Output is correct
3 Correct 15 ms 3668 KB Output is correct
4 Correct 19 ms 3704 KB Output is correct
5 Correct 26 ms 5360 KB Output is correct
6 Correct 16 ms 3660 KB Output is correct
7 Correct 16 ms 3736 KB Output is correct
8 Correct 27 ms 5492 KB Output is correct
9 Correct 31 ms 5316 KB Output is correct
10 Correct 16 ms 3800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 17124 KB Output is correct
2 Correct 163 ms 17000 KB Output is correct
3 Correct 150 ms 17184 KB Output is correct
4 Correct 149 ms 17012 KB Output is correct
5 Correct 145 ms 17148 KB Output is correct
6 Correct 138 ms 17240 KB Output is correct
7 Correct 135 ms 17268 KB Output is correct
8 Correct 138 ms 17132 KB Output is correct
9 Correct 167 ms 17388 KB Output is correct
10 Correct 146 ms 17276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 5308 KB Output is correct
2 Correct 25 ms 5440 KB Output is correct
3 Correct 36 ms 5488 KB Output is correct
4 Correct 24 ms 5456 KB Output is correct
5 Correct 30 ms 5420 KB Output is correct
6 Correct 25 ms 5324 KB Output is correct
7 Correct 27 ms 5348 KB Output is correct
8 Correct 30 ms 5380 KB Output is correct
9 Correct 24 ms 5384 KB Output is correct
10 Correct 24 ms 5344 KB Output is correct
11 Correct 24 ms 3660 KB Output is correct
12 Correct 16 ms 3764 KB Output is correct
13 Correct 15 ms 3668 KB Output is correct
14 Correct 19 ms 3704 KB Output is correct
15 Correct 26 ms 5360 KB Output is correct
16 Correct 16 ms 3660 KB Output is correct
17 Correct 16 ms 3736 KB Output is correct
18 Correct 27 ms 5492 KB Output is correct
19 Correct 31 ms 5316 KB Output is correct
20 Correct 16 ms 3800 KB Output is correct
21 Correct 145 ms 17124 KB Output is correct
22 Correct 163 ms 17000 KB Output is correct
23 Correct 150 ms 17184 KB Output is correct
24 Correct 149 ms 17012 KB Output is correct
25 Correct 145 ms 17148 KB Output is correct
26 Correct 138 ms 17240 KB Output is correct
27 Correct 135 ms 17268 KB Output is correct
28 Correct 138 ms 17132 KB Output is correct
29 Correct 167 ms 17388 KB Output is correct
30 Correct 146 ms 17276 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 0 ms 336 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 324 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 336 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 360 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 110 ms 17048 KB Output is correct
62 Correct 78 ms 17280 KB Output is correct
63 Correct 130 ms 16836 KB Output is correct
64 Correct 97 ms 17200 KB Output is correct
65 Correct 111 ms 17224 KB Output is correct
66 Correct 136 ms 17144 KB Output is correct
67 Correct 114 ms 16944 KB Output is correct
68 Correct 143 ms 17244 KB Output is correct
69 Correct 136 ms 16972 KB Output is correct
70 Correct 149 ms 16868 KB Output is correct