-7

yesterday,we had to solve problems at the codeforces contest I couldn't solve this problem since I am a total beginner.

http://codeforces.com/contest/353/problem/A

I used this algorithm, but something is wrong with it. I think it should print s or f, however it prints nothing. it just auto closes. Even when I added an input to stop instant close

#include <cstdlib>
#include <iostream>

using namespace std;

    int main(){
    int y=0;
    int x=0;
    int f;
    int a;
    cin >> a;
    int s;
    s = 0;
    int number [a][a];
    for(int i = 0;i<a;i++){                
        cin >> number[i][0] >> number[i][1];
        x += number[i][0];
        y += number[i][1];
    }

    for(int i = 0;i<a;i++){
        if(x%2==0 && y%2==0){
            return s;             
        }else if(y%2!=0 && x%2==0){
            f = -1;
            return f;
        }else if(y%2==0 && x%2!=0){
            f = -1;
            return f;
        }else{
            y+= number[i][0];
            x+= number[i][1];
            s++;  
        }  
    }

    int g;
    if(f!=-1){
        cout << s;
    }else{
       cout << f;
    }  
} 
soheils
  • 53
  • 6

2 Answers2

1

As Angew said, the return statements are incorrect and causing you to exit your main. You want to replace this by a break; to exit the loop but not the function.

sarseyn
  • 66
  • 6
  • thanks. the syntax error is solved.however , it prints -1 when it should print 1. – soheils Oct 11 '13 at 13:46
  • is something wrong with my else ifs?thank you – soheils Oct 11 '13 at 13:49
  • Your algorithm is simply incorrect. It quits the for loop as soon as either x or y is odd. – sarseyn Oct 11 '13 at 14:06
  • @soheils As far as I can tell, you never even "flip" a domino. – sarseyn Oct 11 '13 at 14:13
  • the Algorithm isn't wrong. overtly complicated? yes. I considered the impossible situation that >=2 dominos need to be flipped.also, it should quit and cout -1 as soon as one of them is odd and the other isn't, because that means one of them contains an extra odd number which means there can never be a soulution. – soheils Oct 11 '13 at 18:07
0

I have not spent effort in trying to understand your algorithm, but at first glance it looks more complicated than it should be.

From my understanding of the problem, there are 3 possibilities:

  • the totals of the upper halves and the lower halves are already even (so nothing needs to be done)
  • the totals of the upper halves and the lower halves cannot be made even (so no solution exists)
  • just one Domino needs to be rotated to get the totals of the upper halves and the lower halves to be even (so the time needed is 1 second)

I base this on the fact that adding only even numbers always gives an even result, and adding an even number of odd numbers also always gives an even result.

Based on this, instead of having a 2-dimensional array like in your code, I would maintain 2 distinct arrays - one for the upper half numbers and the other for the lower half numbers. In addition, I would write the following two helper functions:

  1. oddNumCount - takes an array as input; simply returns the number of odd numbers in the array.
  2. oddAndEvenTileExists - takes 2 arrays as input; returns the index of the first tile with an odd+even number combination, -1 if no such tile exists.

Then the meat of my algorithm would be:

if (((oddNumCount(upper_half_array) % 2) == 0) && ((oddNumCount(lower_half_array) % 2) == 0))
{
    // nothing needs to be done

    result = 0;
}
else if (((oddNumCount(upper_half_array) - oddNumCount(lower_half_array)) % 2) == 0)
{
    // The difference between the number of odd numbers in the two halves is even, which means a solution may exist.
    // A solution really exists only if there exists a tile in which one number is even and the other is odd.

    result = (oddAndEvenTileExists(upper_half_array, lower_half_array) >= 0) ? 1 : -1;
}
else
{
    // no solution exists.

    result = -1;
}

If you wanted to point out exactly which tile needs to be rotated, then you can save the index that "oddAndEvenTileExists" function returns.

You can write the actual code yourself to test if this works. Even if it doesn't, you would have written some code that hopefully takes you a little above "total beginner".

sarseyn
  • 66
  • 6
Gautam
  • 1,079
  • 14
  • 21
  • thanks. I just got what was wrong with my algorithm. I had complicated it with considering that there can be 2 domino flips as well. which is Mathematically incorrect. I guess I have to pass the blame to "lack of time" or something. I didn't even think about the Math aspect....this needs improvement.also, why are you all making fun of the "total" word? – soheils Oct 11 '13 at 18:09
  • Nobody is making "fun" I think. We're just repeating the poor choice of words for the title of your question. – Gautam Oct 12 '13 at 08:36