QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570930 | #9315. Rainbow Bracket Sequence | zqx | WA | 4ms | 5988kb | C++23 | 2.9kb | 2024-09-17 19:12:51 | 2024-09-17 19:12:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define all(tar) tar.begin(),tar.end()
#define pii pair<int,int>
#define AC return 0;
int n, m, t;
const int INF = 0x3f3f3f3f;
const int maxx = 5e3 + 5;
const int mod = 1e9 + 7;
int cnt = -1, ans, first[10010], dis[10010], vis[10010];
struct edge {
int to, next, w, cap;
} a[200010];
inline void add(int u, int v, int cap, int w) {
a[++cnt] = (edge) {
v, first[u], w, cap
};
first[u] = cnt;
a[++cnt] = (edge) {
u, first[v], -w, 0
};
first[v] = cnt;
}
int q[100010];
bool spfa(int s, int t) {
int head = 0, tail = 1, i, u, v, w;
memset(dis, -1, sizeof(dis));
memset(vis, 0, sizeof(vis));
q[0] = t;
vis[t] = 1;
dis[t] = 0;
while (head < tail) {
u = q[head++];
vis[u] = 0;
for (i = first[u]; ~i; i = a[i].next) {
v = a[i].to;
w = a[i].w;
if (a[i ^ 1].cap && ((dis[v] == -1) || (dis[v] > dis[u] - w))) {
dis[v] = dis[u] - w;
if (!vis[v]) q[tail++] = v, vis[v] = 1;
}
}
}
return ~dis[s];
}
int dfs(int u, int t, int limit) {
if (u == t || !limit) {
vis[u] = 1;
return limit;
}
int i, v, f, flow = 0, w;
vis[u] = 1;
for (i = first[u]; ~i; i = a[i].next) {
v = a[i].to;
w = a[i].w;
if (!vis[v] && (dis[v] == dis[u] - w) && (a[i].cap)) {
if (!(f = dfs(v, t, min(limit, a[i].cap)))) continue;
a[i].cap -= f;
a[i ^ 1].cap += f;
ans += f * w;
flow += f;
limit -= f;
if (!limit) return flow;
}
}
return flow;
}
int zkw(int s, int t) { //zkw费用流
int re = 0;
while (spfa(s, t)) {
vis[t] = 1;
while (vis[t]) {
memset(vis, 0, sizeof(vis));
re += dfs(s, t, 1e15);
}
}
return re;
}
const int N=2005;
int id[N],c1[N],c2[N],val[N],idc[N],col[N],idx;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
while(T--){
int n,m;
cin>>n>>m;
n*=2;
memset(first,-1,sizeof first);
ans=0;
cnt=-1;
idx=0;
for(int i=1;i<=m;i++){
cin>>c1[i];
c2[i]=0;
idc[i]=++idx;
}
for(int i=1;i<=n;i++){
cin>>col[i];
c2[col[i]]++;
}
int res=0;
for(int i=1;i<=n;i++){
cin>>val[i];
id[i]=++idx;
res+=val[i];
}
int s=++idx;
int t=++idx;
add(s,id[n],n/2,0);
bool flag=0;
for(int i=1;i<=m;i++){
if(c1[i]>c2[i]){
flag=0;
break;
}
add(idc[i],t,c2[i]-c1[i],0);
}
if(flag){
cout<<"-1\n";
continue;
}
for(int i=1;i<=n;i++){
add(id[i],idc[col[i]],1,val[i]);
}
for(int i=n-1;i>=1;i--){
add(id[i+1],id[i],i/2,0);
}
int sum=zkw(s,t);
if(sum>=n/2){
cout<<res-ans<<'\n';
}
else cout<<"-1\n";
}
AC
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3936kb
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: 2ms
memory: 4000kb
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: 3ms
memory: 5904kb
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
Wrong Answer
time: 4ms
memory: 5988kb
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 2415940221 2974162077 3476472607 3732904849 3631909633 4479534795 3586223781 3380039505 2946284506 3615784040 -1 -1 -1 4940773463 3195952843 4073152216 4177883697 3398540362 3578975642 4308395607 -1 3078447178 3069102942 3135294...
result:
wrong answer 9th numbers differ - expected: '-1', found: '2415940221'