답안 #656035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656035 2022-11-06T08:56:08 Z 600Mihnea Olympiads (BOI19_olympiads) C++17
100 / 100
26 ms 1900 KB
bool home = 0;
bool verbose;

#include <bits/stdc++.h>

using namespace std;

void print(vector<int> v) {
  cout << " ---> ";
  for (auto &x : v) {
    cout << x << " ";
  }
  cout << "\n";
}

const int N = 500 + 7;
const int K = 7 + 7;
int n;
int k;
int c;
int a[N][K];
vector<int> inds[K];

set<long long> seen;
priority_queue<pair<int, long long>> q;
set<long long> Hashes;

long long get_hash(vector<int> positions) { /// bijection
  assert((int) positions.size() == k);
  long long h = 0;
  for (int i = 0; i < k; i++) {
    h = h * N + positions[i];
  }
  return h;
}

vector<int> get_positions(long long Hash) { /// bijection
  vector<int> positions(k);
  for (int i = k - 1; i >= 0; i--) {
    positions[i] = Hash % N;
    Hash /= N;
  }
  return positions;
}

int get_sum(vector<int> positions) {
  assert((int) positions.size() == k);
  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum +=  a[inds[i][positions[i]]][i];
  }
  return sum;
}

vector<int> Fixed;
vector<int> notFixed;

void bkt(vector<int> current, int position, int sum) {
  if ((int) current.size() == k) {
    sort(current.begin(), current.end());
 //   cout << sum << " ";
 //   print(current);
    long long Hash = get_hash(current);
    assert((int) Hashes.size() < c);
    Hashes.insert(Hash);
    if ((int) Hashes.size() == c) {
      cout << sum << "\n";
      exit(0);
    }
    assert((int) Hashes.size() < c);
    return;
  }
  if (position == (int) notFixed.size()) {
    return;
  }
  assert(0 <= position && position < (int) notFixed.size());
  bkt(current, position + 1, sum);
  current.push_back(notFixed[position]);
  bkt(current, position + 1, sum);
}

void add(vector<int> positions) {
  assert((int) positions.size() == k);
  long long Hash = get_hash(positions);
  if (seen.count(Hash)) {
    return;
  }
  seen.insert(Hash);
  int sum = get_sum(positions);
  q.push({sum, Hash});
}

int main() {
  if (home) {
    verbose = 1;
    freopen ("___input___.txt", "r", stdin);
  } else {
    verbose = 0;
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  }

  cin >> n >> k >> c;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> a[i][j];
    }
  }
  {
    vector<int> biggest;
    for (int c = 0; c < k; c++) {
      for (int i = 1; i <= n; i++) {
        inds[c].push_back(i);
      }
      sort(inds[c].begin(), inds[c].end(), [&] (int i, int j) {
        return a[i][c] > a[j][c];
      });
      biggest.push_back(0);
    }
    add(biggest);
  }
  while (!q.empty()) {
    long long Hash = q.top().second;
    {
        vector<int> positions = get_positions(Hash);
        int sum = q.top().first;
        set<int> sFixed;
        Fixed.clear();
        notFixed.clear();
        for (int c = 0; c < k; c++) {
          sFixed.insert(inds[c][positions[c]]);
        }
        for (auto &i : sFixed) {
          Fixed.push_back(i);
        }
        for (int i = 1; i <= n; i++) {
          if (!sFixed.count(i)) {
            notFixed.push_back(i);
          }
        }
        ///print(Fixed);
        bkt(Fixed, 0, sum);
    }
    q.pop();
    vector<int> positions = get_positions(Hash);
    ///print(positions);
    /// increase one of them

    for (int g = 0; g < k; g++) {
      if (positions[g] + 1 < n) {
        vector<int> np = positions;
        np[g]++;
        add(np);
      }
    }
  }
  cout << "why are you done???\n";
  return 0;
}


/**

**/

Compilation message

olympiads.cpp: In function 'int main()':
olympiads.cpp:96:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     freopen ("___input___.txt", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 6 ms 596 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 7 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 400 KB Output is correct
2 Correct 11 ms 824 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 7 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 724 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 11 ms 956 KB Output is correct
4 Correct 10 ms 1156 KB Output is correct
5 Correct 8 ms 980 KB Output is correct
6 Correct 26 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 6 ms 596 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 7 ms 596 KB Output is correct
5 Correct 4 ms 400 KB Output is correct
6 Correct 11 ms 824 KB Output is correct
7 Correct 5 ms 724 KB Output is correct
8 Correct 7 ms 724 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 11 ms 956 KB Output is correct
12 Correct 10 ms 1156 KB Output is correct
13 Correct 8 ms 980 KB Output is correct
14 Correct 26 ms 1900 KB Output is correct
15 Correct 10 ms 704 KB Output is correct
16 Correct 6 ms 720 KB Output is correct
17 Correct 16 ms 1108 KB Output is correct
18 Correct 19 ms 980 KB Output is correct
19 Correct 2 ms 468 KB Output is correct