Cod sursa(job #1179632)

Utilizator alexandrab0507Alexandra Beldica alexandrab0507 Data 28 aprilie 2014 23:09:27
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int n,m, currentSum = 0, hasNegative = false;
    ifstream in;
    ofstream out;

    in.open("flip.in");
    out.open("flip.out");

    in>>n;
    in>>m;

    int mat[n][m];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            in>>mat[i][j];
            currentSum += mat[i][j];
            if (mat[i][j] < 0) {
                hasNegative = true;
            }
        }

    if (hasNegative == true) {
        for (int j = 0; j < m; j++) {
            int nrNegatives = 0, normalSum = 0, flipSum = 0;
            for (int i = 0; i < n; i++) {
                normalSum += mat[i][j];
                flipSum += (-mat[i][j]);

                if (mat[i][j] < 0) {
                    nrNegatives++;
                }
            }

            if (nrNegatives > n) {
                currentSum -= normalSum;
                currentSum += flipSum;

                for (int i = 0; i < n; i++) {
                    mat[i][j] = -mat[i][j];
                }
            }
        }

        for (int i = 0; i < n; i++) {
            int nrNegatives = 0, normalSum = 0, flipSum = 0;
            for (int j = 0; j < m; j++) {
                normalSum += mat[i][j];
                flipSum += (-mat[i][j]);

                if (mat[i][j] < 0) {
                    nrNegatives++;
                }
            }

            if (nrNegatives > m) {
                currentSum -= normalSum;
                currentSum += flipSum;

                for (int j = 0; j < m; j++) {
                    mat[i][j] = -mat[i][j];
                }
            }
        }

        out<<currentSum;
    }
    else {
        out<<currentSum;
    }

    in.close();
    out.close();
}