Submission #656035

#TimeUsernameProblemLanguageResultExecution timeMemory
656035600MihneaOlympiads (BOI19_olympiads)C++17
100 / 100
26 ms1900 KiB
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 (stderr)

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);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...