QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#575730#9315. Rainbow Bracket SequencexucaiRE 1ms3740kbC++202.9kb2024-09-19 16:30:362024-09-19 16:30:41

Judging History

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

  • [2024-09-19 16:30:41]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3740kb
  • [2024-09-19 16:30:36]
  • 提交

answer

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <numeric>
#include <queue>
#include <cassert>
#include <cstring>
#include <climits>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <array>

using namespace std;
#define int long long

constexpr int N = 3e3, M = 3e3, INF = LLONG_MAX;

int idx, f[M], w[M], e[M], ne[M], h[N];
void add(int a, int b, int c, int d) {
    f[idx] = c, w[idx] = d, e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
    f[idx] = 0, w[idx] = - d, e[idx] = a, ne[idx] = h[b], h[b] = idx ++;
}

int n, m, S, T;
int d[N], inf[N], pre[N];
int spfa() {
    for (int i = 1; i <= T; i += 1) d[i] = INF, inf[i] = pre[i] = 0;
    inf[S] = INF; d[S] = 0;

    queue<int> q; vector inq(N, 0LL);
    q.push(S); inq[S] = 1;
    while (q.size()) {
        int u = q.front(); q.pop(); inq[u] = 0;
        // cout << u << ' ' << h[u] << '\n';

        for (int i = h[u]; i != - 1; i = ne[i]) {
            int v = e[i];
            // cout << v << '\n';
            if (f[i] && d[v] > d[u] + w[i]) {
                d[v] = d[u] + w[i]; inf[v] = min(f[i], inf[u]); pre[v] = i;
                if (inq[v] == 0) {
                    q.push(v); inq[v] = 1;
                }
            }
        }
    }
    return inf[T] > 0;
}

void EK(int &flow, int &cost) {
    while (spfa()) {
        int t = inf[T];
        // cout << t << ' ' << d[T] << '\n';
        flow += t, cost += t * d[T];
        for (int u = T; u != S; u = e[pre[u] ^ 1]) {
            f[pre[u]] -= t;
            f[pre[u] ^ 1] += t;
        }
    }
}

void solve() {
    cin >> n >> m;
    S = 2 * n + m + 1;
    T = 2 * n + m + 2;

    for (int i = 1; i <= T; i += 1) h[i] = - 1;

    vector l(m + 5, 0LL);
    for (int i = 1; i <= m; i += 1) {
        cin >> l[i];
    }

    vector c(2 * n + 10, 0LL);
    map<int, int> mp;
    for (int i = 1; i <= 2 * n; i += 1) {
        cin >> c[i];
        mp[c[i]] += 1;
    }

    vector v(2 * n + 10, 0LL);
    int ans = 0;
    for (int i = 1; i <= 2 * n; i += 1) {
        cin >> v[i];
        ans += v[i];
    }

    for (int i = 2 * n; i >= 1; i -= 1) {
        if (i == 2 * n) {
            add(S, 2 * n, n, 0);
        } else {
            add(i + 1, i, i / 2, 0);
        }
        add(i, 2 * n + c[i], 1, v[i]);
    }
    
    for (int i = 1; i <= m; i += 1) {
        if (mp[i] < l[i]) {
            cout << - 1 << '\n';
            return;
        }
        add(2 * n + i, T, mp[i] - l[i], 0);
    }

    int flow = 0, cost = 0;
    EK(flow, cost);
    if (flow == n) {
        cout << ans - cost << '\n';
    } else {
        cout << - 1 << '\n';
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;

    while (t --) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3696kb

input:

2
3 2
1 2
1 2 2 2 1 2
3 1 4 2 2 1
3 2
2 2
1 2 2 2 1 2
3 1 4 2 2 1

output:

9
-1

result:

ok 2 number(s): "9 -1"

Test #2:

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

input:

50
8 6
0 2 2 0 3 1
3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6
998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375
1 1
1
1 1
343766215 374461155
3 1
2
1 1 1 1 1 1
796323508 303640854 701432076 853325162 610...

output:

-1
343766215
2351080746
3426965219
-1
-1
1351561190
2539318868
1013080942
4656646546
-1
-1
2231197660
2719131728
3983627641
4712174168
-1
1046749330
6115214757
3920988203
-1
3902088946
-1
2566553992
5268471900
5977120748
7505501534
-1
5054275471
1467678317
883992368
1044562986
-1
4024634503
-1
14472...

result:

ok 50 numbers

Test #3:

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

input:

25
20 4
0 0 0 1
2 2 2 2 4 4 4 1 2 2 2 1 3 4 1 3 4 4 3 1 3 2 1 4 2 2 4 1 2 2 3 3 4 1 3 1 4 1 2 1
569363376 821862673 89663213 862407789 442940149 823902948 903631686 838830270 645793571 397350060 806574247 373166844 448916252 435880456 969841293 998227951 276194969 654967687 396909705 696186514 16992...

output:

16418796680
10558213381
-1
1502390329
8534183652
13571062458
8007768610
12656453595
3378659874
8410746077
12352187024
5743130624
5952844559
2285684441
4242613506
836778846
4838639494
8586807028
8535185475
8089358175
5627473863
14399529671
-1
11483578919
4919631490

result:

ok 25 numbers

Test #4:

score: -100
Runtime Error

input:

83
6 5
1 0 1 1 0
2 4 4 5 3 2 4 5 3 3 3 3
597659626 649040970 33207011 223207847 960704874 418600362 658594226 417168695 767527655 622701955 867509363 235369723
6 2
0 0
1 1 2 2 2 2 1 1 1 2 2 1
405752009 976807343 267881918 26193206 947664189 555835767 587219021 231445627 755417826 541362608 846129246...

output:


result: