Cod sursa(job #1014018)

Utilizator razvan9310FMI - Razvan Damachi razvan9310 Data 21 octombrie 2013 23:19:18
Problema Elimin Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;

int M, N, R, C, MAX, total;
vector<vector<int> > mat;
vector<int> sum_row, sum_col;
bitset<8000> sol;
ifstream in("elimin.in");
ofstream out("elimin.out");

void input() {
  in>>M>>N>>R>>C;
  sum_row.resize(M);
  sum_col.resize(N);
  for (int i = 0; i < M; ++i) {
    vector<int> row(N, 0);
    mat.push_back(row);
    int val, sum = 0;
    for (int j = 0; j < N; ++j) {
      in>>val;
      mat[i][j] = val;
      sum += val;
    }
    total += sum;
    sum_row[i] = sum;
  }
}

void columnSort(int total) {
  int i, j;
  for (j = 0; j < N; ++j) {
    int sum = 0;
    for (i = 0; i < M; ++i) {
      if (sol[i] == 0) {
        sum += mat[i][j];
      }
    }
    sum_col[j] = sum;
  }
  sort(sum_col.begin(), sum_col.end());
  for (i = 0; i < C; ++i) {
    total -= sum_col[i];
  }
  if (total > MAX) {
    MAX = total;
  }
}

void back(const int &pos, const int &lvl, const int &total) {
  if (lvl == R) {
    columnSort(total);
    return;
  }
  for (int i = pos + 1; (i < M) && (M - i >= R - lvl); ++i) {
    if (total - sum_row[i] > MAX) {
      sol[i] = 1;
      back(i, lvl + 1, total - sum_row[i]);
      sol[i] = 0;
    }
  }
}

void output() {
  out<<MAX;
}

void solve() {
  back(-1, 0, total);
}

int main() {
  input();
  solve();
  output();
  return 0;
}