(#497) Reorder the Array,Unofficial Tutorial (For Beginners)

Revision en1, by CP_Sucks_GF_Doesnt, 2018-07-14 03:42:48

Hi Guys, Today first time I'm trying to explain my way to solve 3rd problem of CF Round 497. You can Read the question here: http://codeforces.me/contest/1008/problem/C My Approach: Push All element of the given array to Priority Queue. Now, if there is only one element then obviously,it can't replaced by any another integer.

Now taking top 2 elements of the priority queue(q) for comparison, Cases are as follows: int a,b,answer; answer=0; a=q.top(); //1st top q.pop();

while(!q.empty()) { b=q.top(); //2nd top q.pop(); 1. If a>b: then we can replace b by a. answer++; a=b; //(Important Step) Making current element(b) as 1st top for NEXT ITERATION 2. If a<=b: So here at current iteration 'a' isn't useful,,,but it may be useful in future iterations so just store 'a' in a queue(q1), and also check if queue is not empty and front element is greater then 'b' or not if(!q1.empty()): if(q1.front()>b) answer++; q1.pop();

q1.push(a);

} cout<<answer;

Tags #497, priority queue, simple, brute force

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English CP_Sucks_GF_Doesnt 2018-07-14 03:51:03 24
en2 English CP_Sucks_GF_Doesnt 2018-07-14 03:48:40 2
en1 English CP_Sucks_GF_Doesnt 2018-07-14 03:42:48 1208 Initial revision (published)