Submission #395568

#TimeUsernameProblemLanguageResultExecution timeMemory
395568fedoseevtimofey카니발 티켓 (IOI20_tickets)C++14
100 / 100
1027 ms114404 KiB
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
#include "tickets.h"

using namespace std;
 
typedef long long ll;

long long find_maximum(int k, vector <vector <int>> x) {
	int n = x.size();
	int m = x[0].size();
  vector <vector <int>> p(n, vector <int> (m));
  vector <int> cnt(n);
  ll score = 0;
  vector <pair <ll, int>> q;

  auto add = [&] (int id, int j) {
    ll bg = x[id][p[id][m - j - 1]];
    ll sm = x[id][p[id][k - j - 1]];
    q.push_back({bg + sm, id});
  };  

  for (int i = 0; i < n; ++i) {
    iota(p[i].begin(), p[i].end(), 0);
    sort(p[i].begin(), p[i].end(), [&] (int f, int s) {
      return x[i][f] < x[i][s];
    });
    for (int j = 0; j < k; ++j) {
      score -= x[i][p[i][j]];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      add(i, j);
    }
  }
  sort(q.rbegin(), q.rend());
  for (int it = 0; it < k * n / 2; ++it) {
    score += q[it].first;
    ++cnt[q[it].second];
  }
  vector <vector <int>> ans(n, vector <int> (m, -1));
  int r = 0;
  for (int i = 0; i < n; ++i) {
    vector <int> used(k);
    for (int j = 0; j < cnt[i]; ++j) {
      ans[i][p[i][m - j - 1]] = r;
      used[r++] = 1;
      r %= k;
    }
    int uk = 0;
    for (int j = 0; j < k - cnt[i]; ++j) {
      while (used[uk]) ++uk;
      ans[i][p[i][j]] = uk++; 
    }
  }
	allocate_tickets(ans);
  return score;
}

#ifdef LOCAL 

static int n;
static int m;
static int k;
static std::vector<std::vector<int>> d;
static std::vector<std::vector<int>> x;
static int called = 0;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        exit(0);
    }
}


void allocate_tickets( std::vector<std::vector<int>> _d) {
    check(!called, "allocate_tickets called more than once");
    d = _d;
    check((int)d.size() == n, "allocate_tickets called with parameter of wrong size");
    for (int i = 0; i < n; i++) {
        check((int)d[i].size() == m, "allocate_tickets called with parameter of wrong size");
    }
    called = 1;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
    cin >> n >> m >> k;
    x.resize(n);
    for (int i = 0; i < n; i++) {
        x[i].resize(m);
        for (int j=0; j < m; j++) {
          cin >> x[i][j];
        }
    }

    long long answer = find_maximum(k, x);
    check(called, "failure to call allocate_tickets");

    cout << answer << '\n';
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          cout << d[i][j] << ' ';
        }
        cout << '\n';
    }
}
#endif

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...