Cod sursa(job #678217)

Utilizator BogdySSSzekely Bogdan BogdySS Data 11 februarie 2012 10:58:08
Problema Jocul Flip Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<fstream>
using namespace std;
int main()
{
	ifstream fin("flip.in");
	ofstream fout("flip.out");
	int m, n, i, j;
	long long sv, sz, v[260]={0}, z[260], s, s1;
	fin >> n >> m;
	for(i=0;i<m*n;i++)
	{
		fin >> v[i];
		z[i]=v[i];
	}
	//verificam fiecare coloana daca trebuie sau nu comutata
	i=0;
	for(j=1;j<=m;j++)
	{
		s=0;s1=0;
		for(i=j-1;i<=m*n;i=i+m)
		{
			s=s+v[i];
			s1=s1+-1*v[i];
		}
		if(s1>s)
			for(i=j-1;i<=m*n;i=i+m)
				v[i]*=-1;
	}
	//verificam fiecare linie daca trebuie sau nu comutat
	for(j=0;j<n;j++)
	{
	s=0;s1=0;
		for(i=j*m;i<j*m+m;i++)
		{
			s+=v[i];
			s1+=v[i]*-1;
		}
		//fout << "s= " << s << " s1=" << s1 << "\n";
		if(s1>s)
			for(i=j*m;i<j*m+m;i++)
				v[i]*=-1;
	}
	sv=0;
	for(i=0;i<n*m;i++)
		sv+=v[i];
	
	
	
	for(j=0;j<n;j++)
	{
	s=0;s1=0;
		for(i=j*m;i<j*m+m;i++)
		{
			s+=z[i];
			s1+=z[i]*-1;
		}
		//fout << "s= " << s << " s1=" << s1 << "\n";
		if(s1>s)
			for(i=j*m;i<j*m+m;i++)
				z[i]*=-1;
	}
	i=0;
	for(j=1;j<=m;j++)
	{
		s=0;s1=0;
		for(i=j-1;i<=m*n;i=i+m)
		{
			s=s+z[i];
			s1=s1+-1*z[i];
		}
		if(s1>s)
			for(i=j-1;i<=m*n;i=i+m)
				z[i]*=-1;
	}
	sz=0;
	for(i=0;i<n*m;i++)
		sz+=z[i];
	if(sz>sv)
		fout << sz;
	else
		fout << sv;






return 0;
}