Cod sursa(job #1073088)

Utilizator razvan9310FMI - Razvan Damachi razvan9310 Data 5 ianuarie 2014 17:23:33
Problema Elimin Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.76 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <bitset>
#define NMAX 700
using namespace std;

int M, N, R, C, MAX, total;
int mat[NMAX][NMAX];
vector<int> sum_row, sum_col;
bitset<7295> sol;

void input() {
    ifstream in("elimin.in");
    in>>M>>N>>R>>C;

    if (M < N) {
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                in>>mat[i][j];
            }
    }
  } else {
            for (int i = 0; i < M; ++i) {
                for (int j = 0; j < N; ++j) {
                    in>>mat[j][i];
                }
            }

            swap(M, N); swap(R, C);
    }
    sum_col.resize(N, 0);
    for (int i = 0; i < M; ++i) {
    int sum = 0;
    for (int j = 0; j < N; ++j) {
      sum += mat[i][j];
    }
    total += sum;
    sum_row.push_back(sum);
    }
    in.close();
}

void sortescu(int total) {
  int i, j;
  for (i = 0; i < N; ++i) {
    sum_col[i] = 0;
  }
  for (i = 0; i < M; ++i) {
    if (!sol[i]) {
      for (j = 0; j < N; ++j) {
        sum_col[j] += mat[i][j];
      }
    }
  }

  sort(sum_col.begin(), sum_col.end());
  for (i = 0; i < C && total > MAX; ++i) {
    total -= sum_col[i];
  }
  if (total > MAX) {
    MAX = total;
  }
}

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

inline void output() {
  ofstream out("elimin.out");
  out<<MAX;
  out.close();
}

int main() {
  input();
  back(-1, 0, total);
  output();
  return 0;
}