Submission #800945

#TimeUsernameProblemLanguageResultExecution timeMemory
800945becaidoCarnival Tickets (IOI20_tickets)C++17
100 / 100
583 ms64036 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "tickets.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
void allocate_tickets( vector<vector<int>> _x);
#endif

const int SIZE = 1505;

int cnt[SIZE];
bool is[SIZE];
int L[SIZE], R[SIZE];
priority_queue<pair<int, int>> pq;
ll find_maximum(int k, vector<vector<int>> x) {
    int n = x.size();
    int m = x[0].size();
    ll sum = 0;
    vector<vector<int>> ans;
    ans.resize(n, vector<int>(m, -1));
    fill(cnt, cnt + n, k);
    FOR (i, 0, n - 1) {
        sum += accumulate(x[i].begin() + m - cnt[i], x[i].end(), 0ll);
        pq.emplace(-x[i][0] - x[i][m - cnt[i]], i);
    }
    FOR (t, 1, n * k / 2) {
        auto [delta, i] = pq.top();
        pq.pop();
        sum += delta;
        cnt[i]--;
        if (cnt[i] > 0) pq.emplace(-x[i][k - cnt[i]] - x[i][m - cnt[i]], i);
    }
    pq = {};
    FOR (i, 0, n - 1) {
        pq.emplace(cnt[i], i);
        R[i] = m - 1;
    }
    FOR (t, 0, k - 1) {
        fill(is, is + n, 0);
        vector<pair<int, int>> vp;
        FOR (i, 0, n / 2 - 1) {
            auto [c, p] = pq.top();
            pq.pop();
            is[p] = 1;
            ans[p][R[p]--] = t;
            vp.pb(c - 1, p);
        }
        for (auto [c, i] : vp) pq.emplace(c, i);
        FOR (i, 0, n - 1) if (!is[i]) ans[i][L[i]++] = t;
    }
    allocate_tickets(ans);
    return sum;
}

/*
in1
2 3 2
0 2 5
1 1 3
out1
7
0 -1 1
-1 1 0

in2
4 2 1
5 9
1 4
3 6
2 7
out2
12
-1 0
0 -1
0 -1
-1 0
*/

#ifdef WAIMAI
static int n;
static int m;
static int k;
static vector<vector<int>> d;
static vector<vector<int>> x;
static int called = 0;

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

void allocate_tickets( vector<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() {
    assert(scanf("%d %d %d", &n, &m, &k) == 3);
    x.resize(n);
    for (int i = 0; i < n; i++) {
        x[i].resize(m);
        for (int j=0; j < m; j++) {
            assert(scanf("%d", &x[i][j]) == 1);
        }
    }
    fclose(stdin);

    ll ans = find_maximum(k, x);
    check(called, "failure to call allocate_tickets");
    printf("%lld\n", ans);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (j) printf(" ");
            printf("%d", d[i][j]);
        }
        printf("\n");
    }
    fclose(stdout);
    return 0;
}
#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...