QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#572537#9315. Rainbow Bracket SequencePeanut_TangTL 3ms14080kbC++143.4kb2024-09-18 15:11:502024-09-18 15:11:50

Judging History

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

  • [2024-09-18 15:11:50]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:14080kb
  • [2024-09-18 15:11:50]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
const int N = 1e5, M = 1e5;
const ll INF = 1e18;

int n, m, S, T, c[N];

struct costflow {
  int cnt=1,hd[N],nxt[M<<1],to[M<<1]; ll limit[M<<1],cst[M<<1];
  void add(int u, int v, ll w, ll c) {
    nxt[++cnt]=hd[u],hd[u]=cnt,to[cnt]=v,limit[cnt]=w,cst[cnt]=c;
    nxt[++cnt]=hd[v],hd[v]=cnt,to[cnt]=u,limit[cnt]=0,cst[cnt]=-c;
  }
  ll fr[N], fl[N], in[N], dis[N];
  std::pair<ll, ll> mincost(int n, int s, int t) {
    ll flow = 0, cost = 0;
    while(1) { // SPFA
      std::queue<int> q;
      std::fill(dis + 1, dis + n + 1, INF);
      std::fill(in + 1, in + n + 1, 0);
      fl[s] = INF, dis[s] = 0, q.push(s);
      while(!q.empty()) {
        int t = q.front();
        q.pop(), in[t] = 0;
        for(int i = hd[t]; i; i = nxt[i]) {
          int it = to[i], d = dis[t] + cst[i];
          if(limit[i] && d < dis[it]) {
            fl[it] = std::min(limit[i], fl[t]), fr[it] = i, dis[it] = d;
            if(!in[it]) in[it] = 1, q.push(it);
          }
        }
      }
      if(dis[t] >= INF) return std::make_pair(flow, cost);
      flow += fl[t], cost += dis[t] * fl[t];
      for(int u = t; u != s; u = to[fr[u] ^ 1]) limit[fr[u]] -= fl[t], limit[fr[u] ^ 1] += fl[t];
    }
  }
  void init(int n) {
    cnt = 1;
    std::fill(hd + 1, hd + n + 1, 0);
  }
};

struct bounded_costflow {
  int e, u[M], v[M]; ll lo[M], hi[M], cst[M];
  void add(int _u, int _v, ll l, ll r, ll c) {
    if(c < 0) {
      u[++e] = _u, v[e] = _v, lo[e] = r, hi[e] = r, cst[e] = c;
      u[++e] = _v, v[e] = _u, lo[e] = l, hi[e] = r, cst[e] = -c;
    }
    else u[++e] = _u, v[e] = _v, lo[e] = l, hi[e] = r, cst[e] = c;
  }
  costflow g;
  std::pair<ll, ll> mincost(int n, int s, int t) {
    int ss = n + 1, tt = n + 2;
    static ll w[N];
    std::fill(w + 1, w + n + 1, 0);
    g.init(n + 2);
    ll flow = 0, cost = 0, tot = 0;
    for(int i = 1; i <= e; i++) {
      w[u[i]] -= lo[i], w[v[i]] += lo[i];
      cost += lo[i] * cst[i];
      g.add(u[i], v[i], hi[i] - lo[i], cst[i]);
    }
    for(int i = 1; i <= n; i++)
      if(w[i] > 0) g.add(ss, i, w[i], 0), tot += w[i];
      else if(w[i] < 0) g.add(i, tt, -w[i], 0);
    g.add(t, s, 1e8, 0);
    std::pair<ll, ll> res = g.mincost(n + 2, ss, tt);
    if (res.first != tot)
        return {-1, -1}; // 判断是否有可行流
    cost += res.second;
    flow += g.limit[g.hd[s]];
    g.hd[s] = g.nxt[g.hd[s]], g.hd[t] = g.nxt[g.hd[t]];
    res = g.mincost(n + 2, s, t);
    return std::make_pair(flow + res.first, cost + res.second);
  }
  void init() {
    e = 0;
  }
} f;

void Solve() {
    scanf("%d %d", &n, &m);
    n += n;
    S = n + m + 1;
    T = S + 1;
    f.init();
    for (int i = 1; i <= m; i++) {
        int x;
        scanf("%d", &x);
        f.add(S, i, x, n, 0);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
    }
    for (int i = 1; i <= n; i++) {
        int w;
        scanf("%d", &w);
        f.add(c[i], i + m, 0, 1, -w);
    }
    for (int i = 1; i < n; i++) {
        f.add(i + m, i + 1 + m, (i + 1) / 2, i, 0);
    }
    f.add(n + m, T, n / 2, n / 2, 0);

    auto res = f.mincost(n + m + 2, S, T);
    if (res.first == -1 && res.second == -1) {
        puts("-1");
    } else {
        printf("%lld\n", -res.second);
    }
}

int main() {
    int tt;
    scanf("%d", &tt);
    while (tt--) {
        Solve();
    }

    return 0;
}

详细

Test #1:

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

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: 0ms
memory: 14080kb

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: 0ms
memory: 14028kb

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: 0
Accepted
time: 3ms
memory: 14080kb

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:

-1
4518989634
3550182642
4529809699
4042429510
4145000717
-1
3635082691
-1
-1
3476472607
3732904849
3631909633
4479534795
3586223781
3380039505
2946284506
3615784040
-1
-1
-1
4940773463
3195952843
4073152216
4177883697
3398540362
3578975642
4308395607
-1
3078447178
3069102942
3135294474
5256676097
-...

result:

ok 83 numbers

Test #5:

score: -100
Time Limit Exceeded

input:

71
7 4
0 1 0 4
3 4 1 1 4 4 2 4 1 1 1 4 4 4
580852652 638740575 585501313 439482552 637837864 335796176 447934224 259084035 778210267 469729886 908657968 750731414 508195022 701461051
7 6
0 1 1 0 0 1
3 2 4 3 5 3 1 1 5 4 3 1 6 1
198076752 601490845 123074777 392892100 148645775 938575995 355185760 558...

output:


result: