Submission #423812

# Submission time Handle Problem Language Result Execution time Memory
423812 2021-06-11T12:59:50 Z peijar Carnival Tickets (IOI20_tickets) C++17
27 / 100
767 ms 51312 KB
#include <bits/stdc++.h>
using namespace std;
#include "tickets.h"

long long find_maximum(int k, vector<vector<int>> x) {
  int n = x.size();
  int m = x[0].size();
  vector<vector<int>> answer(n, vector<int>(m, -1));

  if (m == 1) {
    assert(k == 1);
    vector<int> elements;
    for (int i(0); i < n; ++i) {
      answer[i][0] = 0;
      elements.push_back(x[i][0]);
    }
    allocate_tickets(answer);

    long long ans = 0;
    sort(elements.begin(), elements.end());
    int mid = elements[n / 2];
    for (int v : elements)
      ans += abs(mid - v);
    return ans;
  } else if (k == 1) {
    vector<pair<int, int>> pairs(n);
    vector<bool> pickSide;
    long long sol = -1;
    for (int i(0); i < n; ++i)
      pairs[i] = make_pair(x[i][0], x[i].back());

    auto calcAns = [&](int mediane) {
      priority_queue<pair<int, int>> bstRemoved;
      vector<bool> side(n);
      long long curSol = 0;
      int neededRight = n / 2;
      for (int i(0); i < n; ++i) {
        if (pairs[i].second < mediane) {
          curSol += mediane - pairs[i].first;
          continue;
        }

        curSol += pairs[i].second - mediane;
        side[i] = 1;
        if (pairs[i].first > mediane)
          neededRight--;
        else
          bstRemoved.emplace(2 * mediane - (pairs[i].first + pairs[i].second),
                             i);
        if (neededRight < 0)
          return;
        while ((int)bstRemoved.size() > neededRight) {
          auto [delta, pos] = bstRemoved.top();
          bstRemoved.pop();
          curSol += delta;
          side[pos] = 0;
        }
      }
      int cnt = 0;
      for (auto v : side)
        cnt += v;
      if (cnt == n / 2 and curSol > sol)
        sol = curSol, pickSide = move(side);
    };

    for (auto [med1, med2] : pairs)
      calcAns(med1), calcAns(med2);
    for (int i(0); i < n; ++i) {
      if (pickSide[i])
        answer[i].back() = 0;
      else
        answer[i][0] = 0;
    }
    allocate_tickets(answer);
    return sol;
  } else if (k == m) {
    vector<tuple<int, int, int>> vals;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        vals.emplace_back(x[i][j], i, j);
    sort(vals.begin(), vals.end());
    vector<vector<bool>> side(n, vector<bool>(m));
    int cnt = 0;
    for (int i = n * m / 2; i < n * m; ++i) {
      auto [v, a, b] = vals[i];
      side[a][b] = 1;
      cnt++;
    }
    assert(2 * cnt == n * m);
    long long ans = 0;
    vector<vector<int>> posLeft(n), posRight(n);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (side[i][j])
          posRight[i].push_back(j), ans += x[i][j];
        else
          posLeft[i].push_back(j), ans -= x[i][j];
      }
    for (int round = 0; round < m; ++round) {
      vector<bool> picked(n);
      int gaucheNec = n / 2, droiteNec = n / 2;
      for (int col = 0; col < n; ++col) {
        if (posLeft[col].empty()) {
          picked[col] = true;
          answer[col][posRight[col].back()] = round;
          posRight[col].pop_back();
          droiteNec--;
        } else {
          picked[col] = true;
          answer[col][posLeft[col].back()] = round;
          posLeft[col].pop_back();
          gaucheNec--;
        }
      }
      for (int col = 0; col < n; ++col) {
        if (picked[col])
          continue;
        assert(!posLeft[col].empty() and !posRight[col].empty());
        if (gaucheNec) {
          answer[col][posLeft[col].back()] = round;
          posLeft[col].pop_back();
          gaucheNec--;
        } else {
          answer[col][posRight[col].back()] = round;
          posRight[col].pop_back();
          droiteNec--;
        }
      }
    }
    for (auto &v : answer)
      for (auto w : v)
        assert(w != -1);
    auto calcAns = [&](vector<int> values) {
      sort(values.begin(), values.end());
      int med = values[n / 2];
      long long ret = 0;
      for (auto v : values) {
        if (v >= med)
          ret += v - med;
        else
          ret += med - v;
      }
      for (int i(0); i < n; ++i) {
        if (i >= n / 2)
          ret -= values[i];
        else
          ret += values[i];
      }
      assert(!ret);
    };
    long long ans2 = 0;
    vector<vector<int>> values(m);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        values[answer[i][j]].push_back(x[i][j]);
    for (auto &v : values)
      calcAns(v);
    allocate_tickets(answer);
    return ans;
  }

  allocate_tickets(answer);
  return 1;
}

Compilation message

tickets.cpp: In function 'long long int find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:151:15: warning: unused variable 'ans2' [-Wunused-variable]
  151 |     long long ans2 = 0;
      |               ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 30 ms 2240 KB Output is correct
6 Correct 767 ms 51312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB There is no ticket of color 0 on day 0
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Contestant returned 11 but the tickets gives a total value of 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Contestant returned 11 but the tickets gives a total value of 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Contestant returned 11 but the tickets gives a total value of 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 3 ms 332 KB Output is correct
11 Correct 30 ms 2240 KB Output is correct
12 Correct 767 ms 51312 KB Output is correct
13 Incorrect 0 ms 204 KB There is no ticket of color 0 on day 0
14 Halted 0 ms 0 KB -