Pagini recente » Borderou de evaluare (job #1181036) | Cod sursa (job #296080) | Cod sursa (job #2935728) | Cod sursa (job #2453058) | Cod sursa (job #903249)
Cod sursa(job #903249)
#include <stdio.h>
#include <vector>
#include <algorithm>
#define min 1000000
using namespace std;
int N,M,t[17][17];
struct p
{
int x,y;
};
long mini,REZ=0;
p mink;
vector <long> found;
void _read()
{
freopen("flip.in","r",stdin);
freopen("flip.out","w",stdout);
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
scanf("%d",&t[i][j]);
}
void flipcol(int m)
{
for(int i=0;i<N;i++)
t[i][m]*=-1;
}
void fliprand(int n)
{
for(int j=0;j<M;j++)
t[n][j]*=-1;
}
int sumcol(int m)
{
long s=0;
for(int i=0;i<N;i++)
s+=t[i][m];
return s;
}
int sumrand(int n)
{
long s=0;
for(int j=0;j<M;j++)
s+=t[n][j];
return s;
}
int main()
{
_read();
int test=N*M; bool toif;
found.push_back(min);
while(test)
{
toif=false;
mini=min;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
if(t[i][j]<mini && find(found.begin(),found.end(),t[i][j])==found.end() ){ mini=t[i][j]; mink.y=j; mink.x=i; found.push_back(mini); toif=true;}
if(toif)
if(sumrand(mink.x)<0 && sumcol(mink.y)<0)
{
if(sumrand(mink.x)<sumcol(mink.y))
fliprand(mink.x);
else flipcol(mink.y);
} else
{
if(sumrand(mink.x)<0)
fliprand(mink.x);
if(sumcol(mink.y)<0)
flipcol(mink.y);
}
test--;
}
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
REZ+=t[i][j];
printf("%ld",REZ);
return 0;
}