Cod sursa(job #3307110)

Utilizator Iustin.DDragusanu Iustin Iustin.D Data 17 august 2025 17:08:27
Problema Jocul Flip Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

ifstream cin("flip.in");
ofstream cout("flip.out");

#define int long long

vector<vector<int> >arr;
vector<vector<int> >ari;
int n,m;

int b2(int x)
{
    //cout<<"a\n";
    vector<int>v;
    int r=0;
    //cout<<"a\n";
    while (x)
    {
        v.push_back(x%2);
        x/=2;
    }
    int yo=v.size();
    //cout<<yo-1;
    for (int i=yo-1; i>=0; i--) r=r*10+v[i];
    return r;
}

int sz(int x)
{
    int cnt=0;
    while (x)
    {
        cnt++;
        x/=10;
    }
    return cnt;
}

int pt(int a,int b)
{
    int aa=a;
    for (int i=1; i<b; i++) aa*=a;
    return aa;
}

void flipc(vector<vector<int> >& v, int x)
{
    for (int i=0; i<n; i++) v[i][x]*=-1;
}

void flipl(vector<vector<int> >& v, int x)
{
    for (int i=0; i<m; i++) v[x][i]*=-1;
}

int32_t main()
{
    int cf=0;
    cin>>n>>m;
    arr.resize(n);
    for (int i=0; i<n; i++) arr[i].resize(m);
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++) cin>>arr[i][j];
    }
    //cout<<"a\n";
    for (int i=0; i<pt(2,m); i++)
    {
        ari=arr;
        //cout<<"a\n";
        int y=b2(i),c=0,cnt=0;
        //cout<<"a\n";
        //cout<<y<<"\n";
        int dn=sz(y);
        while (y)
        {
            c++;
            //cout<<"a\n";
            if (y%10==1) flipc(ari,c+m-dn-1);
            //cout<<"a\n";
            y/=10;
        }
        //cout<<"a\n";
        //cout<<"\n\n";
        for (int j=0; j<n; j++)
        {
            int cm=0,cp=0;
            for (int h=0; h<m; h++)
            {
                if (ari[j][h]<0) cm+=ari[j][h]*(-1);
                else cp+=ari[j][h];
            }
            if (cm>cp) flipl(ari,j);
            cnt+=max(cm-cp,cp-cm);
        }
        //cout<<"a\n";
        if (cnt>cf) cf=cnt;
    }
    cout<<cf;
}