QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397486 | #5308. RPG Pro League | marher | WA | 175ms | 15776kb | C++14 | 4.9kb | 2024-04-24 10:36:43 | 2024-04-24 10:36:44 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+50,M=10000,inf=1e9+7;
int n,num[8],c[N],k,b[N],p[N],mpp[8],m,dfn[N];
ll ans;
map<string,int>mp;
string s[N];
struct edge
{
int v,w,nxt;
};
struct flows
{
int head[M],cnt=1,st,ed,ans,dep[M],cur[M];
edge e[M];vector<int>eg;
void add(int u,int v,int w)
{
e[++cnt]=(edge){v,w,head[u]};head[u]=cnt;
e[++cnt]=(edge){u,0,head[v]};head[v]=cnt;
}
int bfs()
{
queue<int>q;
for(int i=1;i<=ed;i++)dep[i]=0;
dep[st]=1,q.push(st);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(!dep[v]&&e[i].w>0)
q.push(v),dep[v]=dep[u]+1;
}
}
return dep[ed];
}
int dfs(int now,int flow)
{
if(now==ed||!flow)return flow;
int res=0;
for(int &i=cur[now];i;i=e[i].nxt)
{
int v=e[i].v;
if(dep[v]==dep[now]+1&&e[i].w>0)
{
int x=dfs(v,min(flow-res,e[i].w));
if(x>0)
{
e[i].w-=x;e[i^1].w+=x;res+=x;
if(flow==res)return res;
}
}
}
return res;
}
void dinic()
{
while(bfs()>0)
{
for(int i=1;i<=ed;i++)cur[i]=head[i];
ans+=dfs(st,inf);
}
}
int sol()
{
st=12;ed=13;
for(int i=1;i<=7;i++)add(st,i,num[i]);
for(int i=8;i<=11;i++)eg.push_back(cnt+1),add(i,ed,0);
for(int i=2;i<=7;i++)add(i,8,inf);
add(1,11,inf);add(2,10,inf);add(3,9,inf);
add(4,11,inf);add(4,10,inf);
add(5,11,inf);add(5,9,inf);
add(6,10,inf);add(6,9,inf);
add(7,11,inf);add(7,10,inf);add(7,9,inf);
for(int i=1;i<=n;i++)
{
for(auto x:eg)++e[x].w;dinic();
if(ans!=4*i)return i-1;
}
return 114514;
}
}FLOWS;
struct poi
{
int w,x,tim;
friend bool operator<(poi a,poi b)
{
return a.w>b.w;
}
};
struct node
{
queue<int>to[7];int w,ty;
priority_queue<poi>q,rub;
void pop()
{
while(!q.empty()&&(b[q.top().x]||q.top().tim!=dfn[q.top().x]))q.pop();
}
poi cost()
{
pop();return q.empty()?(poi){inf}:q.top();
}
void popto()
{
for(int i=1;i<=4;i++)while(!to[i].empty()&&b[to[i].front()]!=ty)to[i].pop();
}
void rubpop()
{
while(!rub.empty()&&(b[rub.top().x]!=ty||rub.top().tim!=dfn[rub.top().x]))rub.pop();
}
void push(poi x)
{
ans+=x.w;b[x.x]=ty;w++;
for(int i=0;i<3;i++)if(p[x.x]&(1<<i))to[4-i].push(x.x);
if(p[x.x]!=1)to[1].push(x.x);
}
}q[6];
void insert()
{
int ty=0;poi c1=(poi){inf,0};
for(int i=1;i<=4;i++)if(q[i].w<k)
{
poi w=q[i].cost();
if(w.w<c1.w)c1=w,ty=i;
}
int st=0,ed=0;poi c2=(poi){inf,0};
for(int i=1;i<=4;i++)if(q[i].w==k)
{
poi w=q[i].cost();q[i].popto();
if(w.w>=c2.w)continue;
for(int j=1;j<=4;j++)if(i!=j&&q[j].w!=k&&!q[i].to[j].empty())
{
st=i,ed=j;c2=w;
break;
}
}
if(c1.w<=c2.w)
{
q[ty].push(c1);
return;
}
int x=q[st].to[ed].front();
q[st].push(c2);q[ed].push((poi){0,x});--q[st].w;
}
void add(int i)
{
for(int j=0;j<3;j++)if(p[i]&(1<<j))q[4-j].q.push((poi){c[i],i,dfn[i]});
if(p[i]!=1)q[1].q.push((poi){c[i],i,dfn[i]});
}
void init()
{
k=FLOWS.sol();
for(int i=1;i<=n;i++)add(i);
for(int i=1;i<=4;i++)q[i].ty=i;
for(int fl=1;fl<=4*k;fl++)insert();
}
void solve(int x)
{
int ty=0,w=0;
for(int i=1;i<=4;i++)q[i].rubpop();
for(int i=0;i<3;i++)if(p[x]&(1<<i))if(w<-q[4-i].rub.top().w)ty=4-i,w=-q[4-i].rub.top().w;
if(p[x]!=1&&w<-q[1].rub.top().w)ty=1,w=-q[1].rub.top().w;
if(w<=c[x])return;
int t=q[ty].rub.top().x;
b[t]=0;--q[ty].w;add(t);
q[ty].push((poi){c[x]-w,x});
solve(t);
}
main()
{
// freopen("in.txt","r",stdin);
mp["B"]=1;mp["D"]=2;mp["S"]=3;mp["BD"]=4;mp["BS"]=5;mp["DS"]=6;mp["BDS"]=7;
mpp[1]=1;mpp[2]=2;mpp[3]=4;mpp[4]=3;mpp[5]=5;mpp[6]=6;mpp[7]=7;
cin>>n;
for(int i=1;i<=n;i++)cin>>s[i]>>c[i],sort(s[i].begin(),s[i].end()),++num[mp[s[i]]],p[i]=mpp[mp[s[i]]];
init();
for(int i=1;i<=n;i++)if(b[i])q[b[i]].rub.push((poi){-c[i],i,0});
cin>>m;
while(m--)
{
int x,val;
cin>>x>>val;dfn[x]++;
if(b[x])
{
ans-=c[x];c[x]=val;
--q[b[x]].w;b[x]=0;add(x);
insert();cout<<ans<<'\n';
continue;
}
c[x]=val;solve(x);cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9940kb
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: 175ms
memory: 15776kb
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:
40721766963064 40720937650054 40721120372880 40720722768213 40721029809776 40720546822562 40719680807733 40720084910620 40720153432078 40720873952927 40721146683113 40721696875436 40722235723316 40722133896063 40723047443192 40723419347010 40722633976087 40722282806304 40722071958869 40721278798949 ...
result:
wrong answer 22nd numbers differ - expected: '40720280356799', found: '40720280493319'