QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#574406#9315. Rainbow Bracket Sequencefrankly6WA 1ms3824kbC++172.9kb2024-09-18 21:59:232024-09-18 21:59:23

Judging History

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

  • [2024-09-18 21:59:23]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3824kb
  • [2024-09-18 21:59:23]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define int long long
typedef long long ll;
using namespace std;
const int MX=105;
const int inf=0x7fffffff;

int N, M, S, T;
int head[3*MX], cnt=1;
int l[MX], col[2*MX], val[2*MX];
int d[3*MX], dis[3*MX], pre[3*MX];
bool vis[3*MX];
int read()
{
    int r=0, f=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
    return r*f;
}
struct edge{int nxt, to, c, w;}e[30*MX*MX];
inline void ae(int u, int v, int c, int w)
{
    // cout << "ae:" << u << " " << v << " " << c << " " << w << '\n';
    e[++cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt; e[cnt].c=c; e[cnt].w=w;
    e[++cnt].to=u; e[cnt].nxt=head[v]; head[v]=cnt; e[cnt].c=0; e[cnt].w=-w;
}
queue<int> q;
bool spfa()
{
    memset(dis,0x3f,sizeof(dis)); //min cost
    memset(d,0,sizeof(d)); //max flow;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(S); dis[S]=0; d[S]=inf;
    while(!q.empty())
    {
        int x=q.front(); q.pop();
        // cout << "X=" << x << '\n';
        vis[x]=0;
        for(int i=head[x];i;i=e[i].nxt)
        {
            int y=e[i].to;
            if(!e[i].c||dis[y]<=dis[x]+e[i].w) continue;
            dis[y]=dis[x]+e[i].w;
            d[y]=min(d[x],e[i].c);
            pre[y]=i;
            if(vis[y]) continue;
            vis[y]=1; q.push(y);
            // if(dis[y]>-100) cout << "x=" << x << ", y=" << y << ", dis=" << dis[y] << '\n';
        }
    } 
    return d[T]>0;
}
pair<int,long long> EK()
{
    int flow=0; ll cost=0;
    while(spfa())
    {
        // cout << "SPIN\n";
        flow+=d[T]; cost+=(ll)(d[T]*dis[T]);
        for(int i=T;i!=S;i=e[pre[i]^1].to)
        {
            // cout << "i=" << i << '\n';
            e[pre[i]].c-=d[T];
            e[pre[i]^1].c+=d[T];
        }
        // cout << "SPOUT\n";
        // cout << flow << " " << cost << '\n';
    }
    return {flow,cost};
}
signed main()
{
    // freopen("testdata.in","r",stdin);
    int times=read();
    while(times--)
    {
        N=read(); M=read(); S=M+2*N+1; T=S+1;
        int sum=0; ll tar=0;
        for(int i=1;i<=M;i++) 
        {
            l[i]=read(); 
            sum+=l[i];
            ae(S,i,l[i],-inf);
            ae(S,i,N,0);
        }
        tar+=(ll)(sum*(-inf));
        for(int i=1;i<=2*N;i++)
            col[i]=read();
        for(int i=1;i<=2*N;i++) 
        {
            val[i]=read();
            ae(col[i],i+M,1,-val[i]);
        }
        for(int i=1;i<2*N;i++)
        {
            ae(i+M,i+M+1,N,0);
            ae(i+M,i+M+1,(i+1)/2,-inf);
            tar+=((i+1)/2)*(-inf);
        }
        ae(M+2*N,T,N,0);
        ll ans=EK().second;
        if(ans>tar||sum>N) cout << "-1\n";
        else cout << -(ans-tar) << '\n';
        for(int i=1;i<=2*N+M+2;i++) head[i]=0; cnt=1;
    }
    return (0-0);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3656kb

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:

3123982173
343766215
2351080746
3426965219
445652178
-1
1351561190
2539318868
1013080942
4656646546
-1
3111041404
2231197660
2719131728
3983627641
4712174168
-1
1046749330
6115214757
3920988203
1767374520
3902088946
2122695428
2566553992
5268471900
5977120748
7505501534
1656122641
5054275471
1467678...

result:

wrong answer 1st numbers differ - expected: '-1', found: '3123982173'