Asmaa_rashad's blog

By Asmaa_rashad, 11 years ago, In English

262B - Roma and Changing Signs 3125661 this is the problem and my submission please, i want to know why it is WA. thank you :)

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

»
11 years ago, # |
  Vote: I like it +2 Vote: I do not like it

If your program gives a wrong answer on the first sample test, you'd better think of a better solution because this must be a completely wrong algorithm.
By the way, I do realize why you obfuscate your code, but for the start you'd better work on the algorithm, not worry about what happens after you send the code.

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

Your algorithm has several errors.

First, in the case where K < N, you do this...

if(k<n)
        {
            for(int i=0;i<k;i++)
            {
                    sum+=tmp[i]*-1;
            }
        }

And in the end, you have this...

for(int i=0;i<n;i++)
            sum+=tmp[i];
          cout<<sum<<endl;

So you'll end up summing only numbers in the range K+1..N. The correct thing to do, is to do something similar to what you do when K >= N (you change the signs of all negative numbers and count how many changes you've made).

Then, another error is that you have the sort and the check whether (K — c) % 2 != 0 inside the K >= N case, and that check must always be made, not only when K >= N. Consider the following test case...

6 4
-7 -6 -5 10 11 12

K = 4 and N = 6, and you still have to make one additional sign change after changing the signs of all negative numbers.

I made those modifications to your code and it got AC. Here it is -> 3132417