题解:CF2068K Amusement Park Rides
LMJOlivia_uu · · 题解
考虑直接建图跑二分图匹配,左部点表示设备编号,右部点表示时间,显然一个左部点只能对一个右部点,反之同理,如果设备
考虑如果若干设施具有相同的
对于每一种不同周期建立节点
接下来考虑怎么简化有部点(时间)。
对于周期 map<int,vector<int> > mp 维护下一个候选时间是
分类讨论一下可以知道一次加点的新增流量最多为
而每一种周期的所新建的时间点相当于是一段
::::info[code]
const ll N=2005,M=N*N;
const ll inf=1e18;
ll _,n;
ll a[N];
ll p[N],cnt[N];
ll id[N];
map<ll,vector<ll> > mp;
struct Flow
{
struct edge
{
ll to,c,nxt;
} ed[M];
ll head[N*4],work[N*4];
ll toe;
ll dep[N*4];
void init()
{
toe=-1;
memset(head,-1,sizeof head);
}
void adde(ll x,ll y,ll c)
{
ed[++toe]={y,c,head[x]};
head[x]=toe;
ed[++toe]={x,0,head[y]};
head[y]=toe;
}
bool bfs(ll s,ll t)
{
memcpy(work,head,sizeof head);
memset(dep,0x3f,sizeof dep);
queue<ll> q;
dep[s]=0;
q.push(s);
while(!q.empty())
{
ll x=q.front();
q.pop();
for(ll i=head[x];~i;i=ed[i].nxt)
{
ll y=ed[i].to;
if(dep[y]>dep[x]+1&&ed[i].c)
{
dep[y]=dep[x]+1;
q.push(y);
}
}
}
return dep[t]<inf;
}
ll dfs(ll x,ll t,ll lim)
{
if(x==t||!lim)
{
return lim;
}
ll flow=0;
for(ll &i=work[x];~i;i=ed[i].nxt)
{
ll y=ed[i].to;
ll tmp;
if(dep[y]==dep[x]+1&&ed[i].c&&(tmp=dfs(y,t,min(lim,ed[i].c))))
{
flow+=tmp;
lim-=tmp;
ed[i].c-=tmp;
ed[i^1].c+=tmp;
}
if(!lim)
{
break;
}
}
return flow;
}
ll dinic(ll s,ll t)
{
ll mxflow=0;
while(bfs(s,t))
{
mxflow+=dfs(s,t,inf);
}
return mxflow;
}
} F;
void solve()
{
cin>>n;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
ll m=0;
for(ll i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
{
m++;
p[m]=a[i];
cnt[m]=1;
}
else cnt[m]++;
}
F.init();
mp.clear();
ll S=0,T=1,idx=1;
for(ll i=1;i<=m;i++)
{
id[i]=++idx;
F.adde(S,id[i],cnt[i]);
}
for(ll i=1;i<=m;i++)
{
mp[p[i]].push_back(i);
}
ll tot=0,ans=0;
while(tot<n)
{
auto it=mp.begin();
ll tim=it->first;
vector<ll> v=move(it->second);
mp.erase(it);
ll nw=++idx;
F.adde(nw,T,1);
for(ll i:v) F.adde(id[i],nw,1);
ll dlt=F.dinic(S,T);
if(dlt)
{
tot+=dlt;
for(ll i:v)
{
mp[tim+p[i]].push_back(i);
}
}
ans=tim;
}
cout<<ans<<"\n";
}