QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#415615#7185. Poor StudentspandapythonerRE 0ms0kbC++143.9kb2024-05-21 06:13:172024-05-21 06:13:17

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 06:13:17]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-05-21 06:13:17]
  • 提交

answer

#pragma GCC optimize("Ofast,urnoll-loops")

#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


const ll inf = 1e18;
mt19937 rnd(234);


int n, k;
vector<int> p;
vector<vector<ll>> c;
vector<ll> a;
vector<ll> f;
vector<ll> cnt;


vector<int> cp;
vector<ll> ccnt;
vector<ll> cf;


void store() {
    cp = p;
    ccnt = cnt;
    cf = f;
}

void restore() {
    p = cp;
    cnt = ccnt;
    f = cf;
}


ll solve() {
    p.resize(n);
    cnt.assign(k, 0);
    for (int i = 0; i < n; i += 1) {
        p[i] = min_element(all(c[i])) - c[i].begin();
        cnt[p[i]] += 1;
    }
    f.assign(k, 0);
    while (1) {
        vector<int> bad;
        queue<int> q;
        vector<int> is_bad(k, false);
        for (int i = 0; i < k; i += 1) {
            if (cnt[i] > a[i]) {
                q.push(i);
                is_bad[i] = true;
            }
        }
        if (q.empty()) {
            break;
        }
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            bad.push_back(v);
            for (int j = 0; j < k; j += 1) {
                if (is_bad[j] or cnt[j] != a[j]) {
                    continue;
                }
                for (int i = 0; i < n; i += 1) {
                    if (v == p[i] and c[i][j] + f[j] == c[i][v] + f[v]) {
                        is_bad[j] = true;
                        q.push(j);
                        break;
                    }
                }
            }
        }
        auto make_flex = [&](ll dlt, bool go_back) -> bool {
            if (go_back) {
                store();
            }
            bool changed = false;
            for (auto x : bad) {
                f[x] += dlt;
            }
            for (int i = 0; i < n; i += 1) {
                if (!is_bad[p[i]]) {
                    continue;
                }
                for (int j = 0; j < k; j += 1) {
                    if (is_bad[j]) {
                        continue;
                    }
                    if (j == p[i]) {
                        continue;
                    }
                    if (c[i][j] + f[j] <= c[i][p[i]] + f[p[i]]) {
                        if (cnt[j] == a[j]) {
                            changed = true;
                        } else {
                            bool was_bad = false;
                            if (cnt[p[i]] > a[p[i]]) {
                                was_bad = true;
                                cnt[p[i]] -= 1;
                                cnt[j] += 1;
                                p[i] = j;
                            }
                            if (cnt[p[i]] == a[p[i]] && was_bad) {
                                changed = true;
                            }
                        }
                    }
                }
            }
            if (go_back) {
                restore();
            }
            return changed;
            };
        ll l = -1;
        ll r = 1e10;
        while (l + 1 < r) {
            ll m = (l + r) / 2;
            if (make_flex(m, true)) {
                r = m;
            } else {
                l = m;
            }
        }
        
        assert(make_flex(r, false));
    }
    ll rs = 0;
    for (int i = 0; i < n; i += 1) {
        rs += c[i][p[i]];
    }
    return rs;
}

int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n >> k;
    c.assign(n, vector<ll>(k));
    for (int i = 0; i < n; i += 1) {
        for (int j = 0; j < k; j += 1) {
            cin >> c[i][j];
        }
    }
    a.resize(k);
    for (int i = 0; i < k; i += 1) {
        cin >> a[i];
    }
    ll rs = solve();
    cout << rs << "\n";
    return 0;
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

6 2
1 2
1 3
1 4
1 5
1 6
1 7
3 4

output:


result: