Cod sursa(job #1759919)

Utilizator silkMarin Dragos silk Data 19 septembrie 2016 23:44:56
Problema Elimin Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <cstdio>
#include <algorithm>
#define INF 1<<30
#define DIM 10000
using namespace std;

int temp[DIM+1];
int lin[DIM+1];
int col[DIM+1];
int lim[DIM+1];
int a[DIM+1];

int main(){
    freopen("elimin.in","r",stdin);
    freopen("elimin.out","w",stdout);

    int i,j,stemp,ans=-INF,R,C,N,M,sum;

    scanf("%d %d %d %d",&N,&M,&R,&C);
    int v[N+1][M+1];

    for(sum = 0, i = 1; i <= N; ++i)
        for(j = 1; j <= M; ++j)
        {
            scanf("%d",&v[i][j]);
            col[j] += v[i][j];
            lin[i] += v[i][j];
            sum += v[i][j];
        }

    if( N < M )
    {
        for(i = 1; i <= R; ++i) a[i] = i;
        for(i = R; i; --i) lim[i] = N+i-R;
        while(1)
        {
            stemp = sum;
            for(i = 1; i <= M; ++i) temp[i] = col[i];
            for(i = 1; i <= R; ++i)
            {
                for(j = 1; j <= M; ++j)
                {
                    temp[j] = temp[j] - v[ a[i] ][j];
                    stemp = stemp - v[ a[i] ][j];
                }
            }
            sort(temp+1,temp+M+1);
            for(i = 1; i <= C; ++i) stemp = stemp - temp[i];
            if( stemp > ans ) ans = stemp;

            for(i = R; i && a[i]==lim[i]; --i);
            ++a[i];
            if(!i) break;
            for(++i; i <= R; ++i) a[i] = a[i-1]+1;
        }
    }
    else{
        for(i = 1; i <= C; ++i) a[i] = i;
        for(i = C; i; --i) lim[i] = M+i-C;
        while(1)
        {
            stemp = sum;
            for(i = 1; i <= N; ++i) temp[i] = lin[i];
            for(i = 1; i <= C; ++i)
            {
                for(j = 1; j <= N; ++j)
                {
                    temp[j] = temp[j] - v[ a[i] ][j];
                    stemp = stemp - v[ a[i] ][j];
                }
            }
            sort(temp+1,temp+N+1);
            for(i = 1; i <= R; ++i) stemp = stemp - temp[i];
            if( stemp > ans ) ans = stemp;

            for(i = C; i && a[i]==lim[i]; --i);
            ++a[i];
            if(!i) break;
            for(++i; i <= C; ++i) a[i] = a[i-1]+1;
        }
    }


    printf("%d\n",ans);




return 0;
}