QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415613#7185. Poor StudentspandapythonerTL 1ms3500kbC++143.8kb2024-05-21 06:11:362024-05-21 06:11:38

Judging History

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

  • [2024-05-21 06:11:38]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3500kb
  • [2024-05-21 06:11:36]
  • 提交

answer

#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;
            }
        }
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3476kb

input:

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

output:

12

result:

ok answer is '12'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3500kb

input:

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

output:

8

result:

ok answer is '8'

Test #3:

score: -100
Time Limit Exceeded

input:

1000 10
734 303 991 681 755 155 300 483 702 442
237 256 299 675 671 757 112 853 759 233
979 340 288 377 718 199 935 666 576 842
537 363 592 349 494 961 864 727 84 813
340 78 600 492 118 421 478 925 552 617
517 589 716 7 928 638 258 297 706 787
266 746 913 978 436 859 701 951 137 44
815 336 471 720 2...

output:


result: