punk_hazard's blog

By punk_hazard, history, 2 hours ago, In English

I can not understand why my submission 292439267 gives wrong answer while submission 292440081 does not. I am unable to understand the difference where i was wrong

this is the part in my WA submission

		while(!pq.empty() && power < hurdle[i].S)
		{
			power += pq.top();
			pq.pop();
			items++;
		}
		if(pq.empty() && power < hurdle[j].S)
		{
			cout <<  -1 << endl;
			return;
		}

While this below one is of accepted one

		while(power < hurdle[i].S)
		{
			if(!pq.empty())
			{
				power += pq.top();
				pq.pop();
				items++;
			}
			else
			{
				cout <<  -1 << endl;
				return;
			}
		}
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
81 minute(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

you need to check that the pq is not empty because it might be and this will lead to Run time error or wrong answer when doing top() or pop() for an empty pq

  • »
    »
    36 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    i am checking it

    since i only do top() or pop() when !pq.empty is true

    • »
      »
      »
      21 minute(s) ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      typo j->i — hurdle[j].S❌, hurdle[i].S

      • »
        »
        »
        »
        9 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yes, there is typo error.

»
15 minutes ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

apart from typo ->

The first code is always not correct because lets assume a test case where we used all the possible elements in the set/pq and ans exits and the set is now empty but as you have written the check outside so -1 also got executed.....but in the second code it's not the same case as it will either only print a valid ans or -1 !!

  • »
    »
    5 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yes, you are right. Because -1 is considered as true.