Cod sursa(job #2078967)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 30 noiembrie 2017 12:54:02
Problema Elimin Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.98 kb
/// 3 burned, 4 left
#include<bits/stdc++.h>
using namespace std;
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}f("elimin.in");
ofstream g("elimin.out");
int n,m,r,c;
int solmax;
int mat[602][602],sum[602],sum2[602];
int ar[21];
void bkt_lin()
{
    for(int i=1;i<=m;++i)
        for(int j=1;j<=n;++j)
            sum[i]+=mat[j][i];
    for(int i=0;i<(1<<n);++i)
    {
        int poz=0;
        for(int j=0;j<n;++j)
            if(i&(1<<j))
                ar[++poz]=j+1;
        if(poz==r)
        {
            int possum=0;
            for(int j=1;j<=m;++j)
                sum2[j]=sum[j],possum+=sum[j];
            for(int q=1;q<=poz;++q)
                for(int j=1;j<=m;++j)
                    sum2[j]-=mat[ar[q]][j],possum-=mat[ar[q]][j];
            sort(sum2+1,sum2+m+1);
            for(int j=1;j<=c;++j)
                possum-=sum2[j];
            if(possum>solmax)
                solmax=possum;
        }
    }
}
void bkt_col()
{
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            sum[i]+=mat[i][j];
    for(int i=0;i<(1<<m);++i)
    {
        int poz=0;
        for(int j=0;j<m;++j)
            if(i&(1<<j))
                ar[++poz]=j+1;
        if(poz==c)
        {
            int possum=0;
            for(int j=1;j<=n;++j)
                sum2[j]=sum[j],possum+=sum[j];
            for(int q=1;q<=poz;++q)
                for(int j=1;j<=n;++j)
                    sum2[j]-=mat[j][ar[q]],possum-=mat[j][ar[q]];
            sort(sum2+1,sum2+n+1);
            for(int j=1;j<=r;++j)
                possum-=sum2[j];
            if(possum>solmax)
                solmax=possum;
        }
    }
}
int main()
{
    f>>n>>m>>r>>c;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            f>>mat[i][j];
    if(n<m)
        bkt_lin();
    else
        bkt_col();
    g<<solmax;
}