QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397487 | #5308. RPG Pro League | marher | WA | 168ms | 16328kb | C++14 | 5.0kb | 2024-04-24 10:39:28 | 2024-04-24 10:39:29 |
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(m>10)cout<<b[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: 3ms
memory: 11228kb
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: 168ms
memory: 16328kb
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:
4 40721766963064 2 40720937650054 4 40721120372880 0 40720722768213 1 40721029809776 3 40720546822562 2 40719680807733 2 40720084910620 4 40720153432078 1 40720873952927 2 40721146683113 4 40721696875436 1 40722235723316 1 40722133896063 1 40723047443192 4 40723419347010 2 40722633976087 3 407222828...
result:
wrong answer 1st numbers differ - expected: '40721766963064', found: '4'