답안 #884343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
884343 2023-12-07T07:43:52 Z pan Feast (NOI19_feast) C++17
100 / 100
136 ms 16576 KB
#include <iostream>
#include <queue>
#include <vector>
#include <climits>
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
struct node 
{
	ll prev, next, val;
};

struct compare
{
	bool operator() (pi const& a, pi const& b)
	{
		return a.first>b.first;
	}
};

int main()
{
	
	
	
	ll n,k,x;
	cin >> n >> k;
	vector<ll> food;
	for (ll i=0; i<n; ++i)
	{
		cin >> x;
		if (x==0) continue;
		if (!food.empty() && ((x<0 && food.back()<0 ) || (x>0 && food.back()>0)))
		{
			food[food.size()-1]+=x;
		}
		else
		{
			food.push_back(x);
		}
	}
	if (food[0]<0) food.erase(food.begin());
	if (food.back()<0) food.pop_back();
	//for (ll i=0; i<food.size(); ++i) cout << food[i] << endl;
	ll nowk = (food.size()+1)/2;
	priority_queue<pi, vector<pi>, compare> pq;
	vector<node> food2;
	for (ll i=0; i<food.size();i++)
	{
		node y;
		y.prev=i-1, y.next = i+1, y.val = food[i];
		food2.pb(y);
		pq.push(mp(abs(food[i]),i));
	}
	vector<bool> valid(food2.size(), true);
	for (ll i=0; i<nowk-k; ++i)
	{
		//cout << "running " << i << endl;
		while (!valid[pq.top().second] || abs(food2[pq.top().second].val)!=pq.top().first)
		{
			pq.pop();
		}
		ll ind=pq.top().second;
		pq.pop();
		node y= food2[ind];
		//cout << "prev is " << y.prev << "next is " << y.next << endl;
		if (y.prev==-1)
		{
			if (y.val>0)
			{
				ll n = y.next;
				ll nn = food2[n].next;
				if (nn<food2.size()) food2[nn].prev=-1;
				valid[ind] = false;
				valid[n]=false;
				
				
			}
			else
			{
				ll n = y.next;
				ll nn = food2[n].next;
				if (nn<food2.size()) food2[nn].prev=ind;
				food2[ind].val+=food2[n].val;
				food2[ind].prev = -1;
				food2[ind].next = nn;
				valid[n] = false;
				pq.push(mp(abs(food2[ind].val), ind));
			}
		}
		else if (y.next==food2.size())
		{
			if (y.val>0)
			{
				ll p = y.prev;
				ll pp = food2[p].prev;
				if (pp>=0) food2[pp].next=food2.size();
				valid[ind] = false;
				valid[p]= false;
			}
			else
			{
				ll p = y.prev;
				ll pp = food2[p].prev;
				if (pp>=0) food2[pp].next=ind;
				food2[ind].val+=food2[p].val;
				food2[ind].next = food2.size();
				food2[ind].prev = pp;
				valid[p] = false;
				pq.push(mp(abs(food2[ind].val), ind));
			}
		}
		else
		{
			ll p = y.prev, n = y.next;
			ll nn = food2[n].next;
			ll pp = food2[p].prev;
			if (pp>=0) food2[pp].next=ind;
			if (nn<food2.size()) food2[nn].prev=ind;
			food2[ind].val+=food2[p].val+food2[n].val;
			food2[ind].prev = pp;
			food2[ind].next = nn;
			valid[p] = false;
			valid[n]=false;
			pq.push(mp(abs(food2[ind].val), ind));

		}
		//for (ll i=0; i<food2.size(); ++i) cout << food2[i].val  << endl;
		//cout << "Now pointing to " << food2[ind].prev  << " and " <<food2[ind].next << endl;
		
	}
	
	ll ans=0;
	
	for (ll i=0; i<food2.size(); ++i) 
	{
		if (valid[i] && food2[i].val>0) {ans+=food2[i].val;}
	}
	cout << ans;

	return 0;
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:50:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |  for (ll i=0; i<food.size();i++)
      |               ~^~~~~~~~~~~~
feast.cpp:75:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     if (nn<food2.size()) food2[nn].prev=-1;
      |         ~~^~~~~~~~~~~~~
feast.cpp:85:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     if (nn<food2.size()) food2[nn].prev=ind;
      |         ~~^~~~~~~~~~~~~
feast.cpp:93:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |   else if (y.next==food2.size())
      |            ~~~~~~^~~~~~~~~~~~~~
feast.cpp:121:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |    if (nn<food2.size()) food2[nn].prev=ind;
      |        ~~^~~~~~~~~~~~~
feast.cpp:137:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |  for (ll i=0; i<food2.size(); ++i)
      |               ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 348 KB Output is correct
2 Correct 79 ms 348 KB Output is correct
3 Correct 80 ms 344 KB Output is correct
4 Correct 80 ms 596 KB Output is correct
5 Correct 80 ms 344 KB Output is correct
6 Correct 77 ms 344 KB Output is correct
7 Correct 101 ms 500 KB Output is correct
8 Correct 79 ms 408 KB Output is correct
9 Correct 84 ms 408 KB Output is correct
10 Correct 78 ms 412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 348 KB Output is correct
2 Correct 43 ms 348 KB Output is correct
3 Correct 40 ms 344 KB Output is correct
4 Correct 44 ms 348 KB Output is correct
5 Correct 79 ms 500 KB Output is correct
6 Correct 41 ms 348 KB Output is correct
7 Correct 44 ms 348 KB Output is correct
8 Correct 82 ms 408 KB Output is correct
9 Correct 77 ms 408 KB Output is correct
10 Correct 41 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 13124 KB Output is correct
2 Correct 124 ms 12064 KB Output is correct
3 Correct 125 ms 12228 KB Output is correct
4 Correct 122 ms 13228 KB Output is correct
5 Correct 127 ms 13240 KB Output is correct
6 Correct 125 ms 11964 KB Output is correct
7 Correct 125 ms 13244 KB Output is correct
8 Correct 122 ms 13072 KB Output is correct
9 Correct 128 ms 13244 KB Output is correct
10 Correct 124 ms 12732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 404 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 404 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 348 KB Output is correct
2 Correct 79 ms 348 KB Output is correct
3 Correct 80 ms 344 KB Output is correct
4 Correct 80 ms 596 KB Output is correct
5 Correct 80 ms 344 KB Output is correct
6 Correct 77 ms 344 KB Output is correct
7 Correct 101 ms 500 KB Output is correct
8 Correct 79 ms 408 KB Output is correct
9 Correct 84 ms 408 KB Output is correct
10 Correct 78 ms 412 KB Output is correct
11 Correct 41 ms 348 KB Output is correct
12 Correct 43 ms 348 KB Output is correct
13 Correct 40 ms 344 KB Output is correct
14 Correct 44 ms 348 KB Output is correct
15 Correct 79 ms 500 KB Output is correct
16 Correct 41 ms 348 KB Output is correct
17 Correct 44 ms 348 KB Output is correct
18 Correct 82 ms 408 KB Output is correct
19 Correct 77 ms 408 KB Output is correct
20 Correct 41 ms 344 KB Output is correct
21 Correct 136 ms 13124 KB Output is correct
22 Correct 124 ms 12064 KB Output is correct
23 Correct 125 ms 12228 KB Output is correct
24 Correct 122 ms 13228 KB Output is correct
25 Correct 127 ms 13240 KB Output is correct
26 Correct 125 ms 11964 KB Output is correct
27 Correct 125 ms 13244 KB Output is correct
28 Correct 122 ms 13072 KB Output is correct
29 Correct 128 ms 13244 KB Output is correct
30 Correct 124 ms 12732 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 404 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 2 ms 348 KB Output is correct
57 Correct 2 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 500 KB Output is correct
61 Correct 105 ms 16056 KB Output is correct
62 Correct 105 ms 15288 KB Output is correct
63 Correct 116 ms 16576 KB Output is correct
64 Correct 102 ms 15872 KB Output is correct
65 Correct 106 ms 16316 KB Output is correct
66 Correct 106 ms 16060 KB Output is correct
67 Correct 106 ms 16052 KB Output is correct
68 Correct 128 ms 15444 KB Output is correct
69 Correct 122 ms 15632 KB Output is correct
70 Correct 126 ms 16180 KB Output is correct