QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#583696#9315. Rainbow Bracket SequenceplanckconstantWA 1ms3620kbC++142.8kb2024-09-22 21:16:302024-09-22 21:16:32

Judging History

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

  • [2024-09-22 21:16:32]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3620kb
  • [2024-09-22 21:16:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 500
int n,m;
int sum=0;
int dis[N],pre[N],preve[N];
struct edge{
    int to,cost,capacity,rev;
};
vector<edge>e[N];
void add(int from,int to,int cost,int capacity)
{
    e[from].push_back({to,cost,capacity,(int)e[to].size()});
    e[to].push_back({from,-cost,0,(int)e[from].size()-1});
}
bool spfa(int s,int t,int cnt)
{
    vector<bool>inq(N);
    memset(pre,-1,sizeof(pre));
    for(int i=1;i<=cnt;i++){
        dis[i]=INF;
        inq[i]=0;
    }
    dis[s]=0;
    queue<int>Q;
    Q.push(s);
    inq[s]=1;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        for(int i=0;i<e[u].size();i++){
            if(e[u][i].capacity>0){
                int v=e[u][i].to;
                int cost=e[u][i].cost;
                if(dis[u]+cost<dis[v]){
                    dis[v]=dis[u]+cost;
                    pre[v]=u;
                    preve[v]=i;
                    if(!inq[v]){
                        inq[v]=1;
                        Q.push(v);
                    }
                }
            }
        }
    }
    return dis[t]!=INF;
}
void mincost(int s,int t,int cnt)
{
    int cost=0;
    int re=0;
    while(spfa(s,t,cnt)){
        int v=t,flow=INF;
        while(pre[v]!=-1){
            int u=pre[v],i=preve[v];
            flow=min(flow,e[u][i].capacity);
            v=u;
        }
        v=t;
        while(pre[v]!=-1){
            int u=pre[v],i=preve[v];
            e[u][i].capacity-=flow;
            e[v][e[u][i].rev].capacity+=flow;
            v=u;
        }
        cost+=dis[t]*flow;
        re+=flow;
    }
    if(re<n/2){
        cout<<-1<<endl;
    }
    else{
        cout<<sum-cost<<endl;
    }
}
int cnt[N];
int col[N];
int val[N];
void slove()
{
    cin>>n>>m;
    n*=2;
    for(int i=1;i<=n+m+5;i++){
        e[i].clear();
    }
    int s,t;
    s=1,t=2;
    sum=0;
    for(int i=1;i<=m;i++){
        cin>>cnt[i];
        cnt[i]=-cnt[i];
    }
    for(int i=1;i<=n;i++){
        cin>>col[i];
        cnt[col[i]]++;
    }
    for(int i=1;i<=n;i++){
        cin>>val[i];
        sum+=val[i];
    }
    add(s,n+2,0,n/2);
    for(int i=n;i>=1;i--){
        add(i+2,n+2+col[i],val[i],1);
        if(i!=1){
            add(i+2,i+1,0,(i-1)/2);
        }
    }
    bool ok=0;
    for(int i=1;i<=m;i++){
        if(cnt[i]<0){
            ok=1;
        }
        else{
            add(n+2+i,t,0,cnt[i]);
        }
    }
    if(ok)
        cout<<-1<<endl;
    else{
        mincost(s,t,n+m+5);
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        slove();
    }
}

詳細信息

Test #1:

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

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: -100
Wrong Answer
time: 1ms
memory: 3572kb

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
-1943886550
-868002077
-1
-1
1351561190
-1755648428
1013080942
-1
-1
-1
-2063769636
-1575835568
-311339655
417206872
-1
1046749330
1820247461
-373979093
-1
-392878350
-1
-1728413304
-1
1682153452
-1084433058
-1
759308175
1467678317
883992368
1044562986
-1
-270332793
-1
1447294256
-18323...

result:

wrong answer 3rd numbers differ - expected: '2351080746', found: '-1943886550'