Pagini recente » Cod sursa (job #3226525) | Cod sursa (job #2526856) | Cod sursa (job #1361144) | Cod sursa (job #2036601) | Cod sursa (job #1127712)
//
// main.cpp
// Flip
//
// Created by Nagy Dani on 2014.02.27..
// Copyright (c) 2014 Nagy Dani. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
std::vector<int>sums;
int N,M;
int **mat;
using namespace std;
void beolvas(int &n,int&m, int***a){
FILE* in = fopen("flip.in","r");
fscanf(in,"%d %d",&n,&m);
*a = (int**)malloc( (n)*sizeof(int*));
for(int i=0;i<n;i++){
*((*a) + i) = (int*)malloc( (m)*sizeof(int) );
for(int j=0;j<m;j++){
fscanf(in,"%d",(*((*a)+i)+j) );
}
}
}
void negaterow(int i){
for(int j=0;j<M;j++) mat[i][j] = -1*mat[i][j];
}
void negatecol(int i){
for(int j=0;j<N;j++) mat[j][i] = -1*mat[j][i];
}
int sumrow(int i){
int s = 0;
for(int j=0;j<M;j++) s+= mat[i][j];
return s;
}
int sumcol(int i){
int s = 0;
for(int j=0;j<N;j++) s+= mat[j][i];
return s;
}
int negsumrow(int i){
int s = 0;
for(int j=0;j<M;j++) s+= -mat[i][j];
return s;
}
int negsumcol(int i){
int s = 0;
for(int j=0;j<N;j++) s+= -mat[j][i];
return s;
}
int sum(){
int s = 0;
for(int i = 0;i<N;i++){
for(int j = 0;j<M;j++){
s+=mat[i][j];
}
}
return s;
}
int megold(){
int old = sum();
//printf("%d",old);
for(int i=0; i< N;i++){
sums.push_back(old + negsumrow(i));
}
for(int i=0; i< M;i++){
sums.push_back(old + negsumcol(i));
}
for(int i=0; i < N;i++){
negaterow(i);
sums.push_back(sum());
}
for(int i=0; i < N;i++){
negaterow(i);
sums.push_back(sum());
}
for(int i=0; i < M;i++){
negatecol(i);
sums.push_back(sum());
}
for(int i=0; i < M;i++){
negatecol(i);
sums.push_back(sum());
}
std::vector<int>::iterator it = std::max_element(sums.begin(),sums.end());
return (*it);
}
int main()
{
FILE * out = fopen("flip.out", "w");
//fprintf(out, "szarik");
beolvas(N,M,&mat);
fprintf(out,"%d",megold());
//printf("%d",556);
return 0;
}