D. Armchairs
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
There are nn armchairs, numbered from 11 to nn from left to right. Some armchairs are occupied by people (at most one person per armchair), others are not. The number of occupied armchairs is not greater than n2n2.
For some reason, you would like to tell people to move from their armchairs to some other ones. If the ii-th armchair is occupied by someone and the jj-th armchair is not, you can tell the person sitting in the ii-th armchair to move to the jj-th armchair. The time it takes a person to move from the ii-th armchair to the jj-th one is |i−j||i−j| minutes. You may perform this operation any number of times, but these operations must be done sequentially, i. e. you cannot tell a person to move until the person you asked to move in the last operation has finished moving to their destination armchair.
You want to achieve the following situation: every seat that was initially occupied must be free. What is the minimum time you need to do it?
Input
The first line contains one integer nn (2≤n≤50002≤n≤5000) — the number of armchairs.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1). ai=1ai=1 means that the ii-th armchair is initially occupied, ai=0ai=0 means that it is initially free. The number of occupied armchairs is at most n2n2.
Output
Print one integer — the minimum number of minutes you have to spend to achieve the following situation: every seat that was initially occupied must be free.
Examples
input
Copy
7 1 0 0 1 0 0 1
output
Copy
3
input
Copy
6 1 1 1 0 0 0
output
Copy
9
input
Copy
5 0 0 0 0 0
output
Copy
0
Note
In the first test, you can perform the following sequence:
- ask a person to move from armchair 11 to armchair 22, it takes 11 minute;
- ask a person to move from armchair 77 to armchair 66, it takes 11 minute;
- ask a person to move from armchair 44 to armchair 55, it takes 11 minute.
In the second test, you can perform the following sequence:
- ask a person to move from armchair 11 to armchair 44, it takes 33 minutes;
- ask a person to move from armchair 22 to armchair 66, it takes 44 minutes;
- ask a person to move from armchair 44 to armchair 55, it takes 11 minute;
- ask a person to move from armchair 33 to armchair 44, it takes 11 minute.
In the third test, no seat is occupied so your goal is achieved instantly.
=========================================================================
网上是有思维加DP的解法的,但是有些不太好想,直接上网络流即可
dinic 在一般图的复杂度为 n^2 * m ,可以处理1e4-1e5级别的图
在处理二分图时(源点-左集-右集-汇点),只需要 sqrt(n)*m的复杂度
而本题恰好是二分图,用网络流甚至比DP要快速
建图方式就是 源点连接人,费用为0,流量为1,每个座位之间连接一个,费用为1,流量为无穷的点, 空位和汇点连接费用为0,流量为1的边,这样就保证了每个人都最多只有一条边流出去,每个空位置最多只有一条边流进来,比DP好理解一些
# include<iostream>
# include<cstring>
# include<queue>
using namespace std;
int n,st,ed,len=2;
typedef long long int ll;
typedef struct
{
int b,e;
ll flow,dis;
}xinxi;
xinxi s[101010];
int f[101010],nex[101010];
int pre[101010],vis[101010];
ll dis[101010],minn[101010];
ll mincost, maxflow;
queue<int>q;
void add(int x,int y,ll flow,ll dis)
{
s[len].b=x;
s[len].e=y;
s[len].flow=flow;
s[len].dis=dis;
nex[len]=f[x];
f[x]=len;
len++;
}
ll inf=1e18;
bool SPFA()
{
while(!q.empty())
q.pop();
for(int i=0;i<=ed;i++)
{
vis[i]=0;
dis[i]=inf;
}
q.push(st);
minn[st]=inf;
dis[st]=0;
vis[st]=1;
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
int x=f[now];
while(x!=-1)
{
int j=s[x].e;
if(s[x].flow==0)
{
x=nex[x];
continue;
}
if(dis[j]>dis[now]+s[x].dis)
{
dis[j]=dis[now]+s[x].dis;
minn[j]=min(minn[now],s[x].flow);
pre[j]=x;
if(!vis[j])
{
vis[j]=1;
q.push(j);
}
}
x=nex[x];
}
}
return (dis[ed]!=inf);
}
void work()
{
while(SPFA())
{
int x=ed;
maxflow+=minn[ed];
mincost+=minn[ed]*dis[ed];
int i;
while(x!=st)
{
i=pre[x];
s[i].flow-=minn[ed];
s[i^1].flow+=minn[ed];
x=s[i^1].e;
}
}
}
int a[5010];
int main ()
{
memset(f,-1,sizeof(f));
cin>>n;
st=0;
ed=n+1;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==1)
{
add(st,i,1,0);
add(i,st,0,0);
}
else
{
add(i,ed,1,0);
add(ed,i,0,0);
}
}
for(int i=1;i<=n;i++)
{
if(i+1<=n)
{
add(i,i+1,inf,1);
add(i+1,i,0,-1);
}
if(i-1>=1)
{
add(i,i-1,inf,1);
add(i-1,i,0,-1);
}
}
work();
cout<<mincost<<endl;
return 0;
}