Tmmaster's blog

By Tmmaster, history, 11 days ago, In English

Hello Codeforces! I've been struggling with this problem for half an hour, I've been testing it but I couldn't find the wrong one. Can you help me? UPD: I solved the problem,Thanks. Reason: I couldn't get all the positive numbers.

  • Vote: I like it
  • -3
  • Vote: I do not like it

»
11 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Though I really can't understand what you are trying to do but may be you are thinking too much.

  • if its a bonus card store it somewhere.

  • if its a hero card pick the max bonus card and give it to hero and discard that card.

The main vision of the problem is make sure you know how to pick max element of a deck(List) in most suitable time complexity. Just think of a data structure that does the job.

»
11 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Your code gives the wrong answer for the following test case:

7
3 3 3 0 0 1 0

Instead of doing what you're doing, it'd be simpler to simply maintain a priority queue / multiset of all elements and remove the largest one everytime you encounter a hero. Like this.

»
11 days ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

The main approach in this question is that as we are getting the positive values we will store them in a priority queue so that maximum remain at the top , but as soon a 0 is encountered, then we get the top value of pq and add that to our result and then discard it, now the reason we are taking a priority queue for this question is that since we want the maximum score, so essentially we are discarding the ones that are least and are not needed. Now you may be thinking instead of taking priority queue why don't we just use a max variable that will store the maximum of values in a sequence where 0 is not there and then as soon as 0 comes we will add the max to our result, but the reason this approach will fail is because, if say two consecutive 0's comes, then for the first 0 we will use the max, but for the second 0 we won't have any data about the second maximum, similarly if a lot of 0s comes then we will need to store data of all values in decreasing order, and hence it is better to just use priority queue for faster insertion and deletion.

here is the code for this question, -------------------------------------------------------------------------------------------------------------- void solve(){ ll n; cin>>n; priority_queue pq; ll ans = 0; for(int i=0; i<n; i++){ ll val; cin>>val; if(val == 0){ if(!pq.empty()){ ans += pq.top(); pq.pop(); } } else{ pq.push(val); } } cout<<ans<<endl; }

»
11 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Tmmaster (previous revision, new revision, compare).