QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282356#5308. RPG Pro LeagueCryingWA 206ms14084kbC++174.3kb2023-12-11 21:01:102023-12-11 21:01:10

Judging History

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

  • [2023-12-11 21:01:10]
  • 评测
  • 测评结果:WA
  • 用时:206ms
  • 内存:14084kb
  • [2023-12-11 21:01:10]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const ll MAXN = 1e5+10,M = 3,INF = 1e18;
void tomin(ll& x,ll y){x = min(x,y);}
void tomax(ll& x,ll y){x = max(x,y);}

ll n,q,s[MAXN],p[MAXN],ww[MAXN],w[MAXN],sum,ans;
char str[M+5];

struct comp{
    bool operator()(int a,int b)const{
        if(w[a] ^ w[b])return w[a] < w[b];
        else return a<b;
    }
};
set<int>f[M+5][M+5],g[M+5],h[M+5];

int tag[MAXN];
void link(int x,int y){
    //printf("link %d->%d\n",x,y);
    assert(tag[y] == -1);
    assert(p[y]>>x&1);
    tag[y] = x; h[x].insert(y);
    rep(i,0,M)if(p[y]>>i&1){
        g[i].erase(y);
        if(i^x)f[x][i].insert(y);
    }
}
void rlink(int x,int y){
    //printf("rlink %d->%d\n",x,y);
    assert(tag[y] == x);
    tag[y] = -1; h[x].erase(y);
    rep(i,0,M)if(p[y]>>i&1){
        g[i].insert(y);
        if(i^x)f[x][i].erase(y);
    }
}
//

int E(int x,int y){return !f[y][x].empty();}
int vis[M+5][M+5],pre[M+5][M+5];

queue<int>qu;

void bfs(){
    memset(vis,0,sizeof vis);memset(pre,0,sizeof pre);
    rep(i,0,M){
        vis[i][i] = 1;qu.push(i);
        while(qu.size()){
            int u=qu.front(); qu.pop();
            rep(v,0,M)if(E(u,v) && !vis[i][v]){
                vis[i][v] = 1;pre[i][v] = u;
                qu.push(v);
            }
        }
    }
}

int path[MAXN],len; 
void walk(int x,int y){ //反转x->y的路径
    len = 0;
    for(int z=y;z^x;z=pre[x][z])path[++len] = z;path[++len] = x; reverse(path+1,path+1+len);
    rep(i,1,len-1){
        int id = *f[path[i+1]][path[i]].begin();
        rlink(path[i+1],id);
        link(path[i],id);
    }
}

void mdf(ll x,ll y){
    sum += (tag[x] != -1) * (-w[x] + y);
    if(tag[x] == -1){
        rep(i,0,M)if(p[x]>>i&1)g[i].erase(x);
    }else{
        h[tag[x]].erase(x);
        rep(i,0,M)if((p[x]>>i&1) && (i^tag[x]))f[tag[x]][i].erase(x);
    }
    w[x] = y;
    if(tag[x] == -1){
        rep(i,0,M)if(p[x]>>i&1)g[i].insert(x);
    }else{
        h[tag[x]].insert(x);
        rep(i,0,M)if((p[x]>>i&1) && (i^tag[x]))f[tag[x]][i].insert(x);
    }
    //
    ll val = 0,u = -1,v = -1;
    bfs();
    if(tag[x] == -1){
        rep(i,0,M)rep(j,0,M)if(vis[i][j] && h[i].size() && (p[x]>>j&1)){
            int z = *h[i].rbegin();
            ll cost = w[x]-w[z];
            if(cost<val)val=cost,u=i,v=j;
        }

        if(val == 0)return;
        assert(u>=0 && v>=0 && val < 0); sum += val;
        int z = *h[u].rbegin();
        rlink(u,z);
        walk(u,v);
        link(v,x);
    }else{
        rep(i,0,M)rep(j,0,M)if(vis[i][j] && tag[x]==i && g[j].size()){
            int z = *g[j].begin();
            ll cost = -w[x]+w[z];
            if(cost<val)val=cost,u=i,v=j;
        }

        if(val == 0)return;
        assert(u>=0 && v>=0 && val < 0); sum += val;
        int z = *g[v].begin();
        rlink(u,x);
        walk(u,v);
        link(v,z);
    }
}


int main(){
    //freopen("h.in","r",stdin);
    ios::sync_with_stdio(false); cin.tie(0);

    cin>>n; 
    rep(i,1,n){
        cin>>str; tag[i] = -1;
        for(int j=0;str[j];j++){
            if(str[j]=='D')p[i] |= 2;
            if(str[j]=='S')p[i] |= 4;
            if(str[j]=='B')p[i] |= 8;
        }
        if(p[i]&6)p[i] |= 1;
        cin>>ww[i];

        rep(j,0,M)if(p[i]>>j&1)g[j].insert(i);
    }
    //任意跑出一组最大流
    rep(x,1,n/4){
        rep(i,0,M){
            bfs(); 
            int flag=0;
            rep(j,0,M)if(g[j].size() && vis[i][j]){
                walk(i,j);
                link(j,*g[j].begin());
                flag=1;break;
            }
            if(!flag)goto END;
        }
    }
    END:;   

    ans=INF;
    rep(i,0,M)tomin(ans,h[i].size());
    rep(i,0,M)if(h[i].size() > ans){
        int j = *h[i].begin();
        rlink(i,j);
    }   
    //

    rep(i,1,n){
        mdf(i,ww[i]);
    }

    cin>>q;
    rep(i,1,q){
        int x,y; cin>>x>>y;
        mdf(x,y);
        cout<<sum<<"\n";
    }

    return 0;
}

詳細信息

Test #1:

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

input:

5
BS 3
D 3
B 2
D 4
D 5
2
5 2
1 5

output:

10
12

result:

ok 2 number(s): "10 12"

Test #2:

score: -100
Wrong Answer
time: 206ms
memory: 14084kb

input:

100000
B 727564174
S 231637747
D 246625257
S 975124756
D 844292741
S 809641006
B 396062588
B 883224128
B 311130283
S 434433581
S 509817048
S 993501344
S 508399542
B 279564062
D 950946642
D 819528264
D 412285620
B 968540732
S 953428318
D 902181790
S 172330493
D 725075
B 135580649
D 714478555
B 466916...

output:

44398775819983
44397946506973
44398129229799
44398129229799
44398436271362
44397953284148
44397087269319
44397491372206
44397559893664
44398280414513
44398553144699
44398553144699
44399091992579
44398990165326
44399903712455
44400522189138
44399736818215
44399385648432
44399174800997
44398381641077
...

result:

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