D. Armchairs - 网络流 费用流,Dinic,二分图匹配

文章描述了一个关于重新安排椅子上的人以达到所有初始被占椅子变为空的问题。给定每把椅子的状态和最多的人数,目标是在最短时间内让所有人移动到空椅子上。解决方案涉及使用网络流算法,特别是二分图的Dinic算法,因为问题可以转换为在网络中寻找最小割,从而确定最小的总移动时间。示例展示了不同情况下的最优移动序列和所需时间。
摘要由CSDN通过智能技术生成

Problem - D - Codeforces

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:

  1. ask a person to move from armchair 11 to armchair 22, it takes 11 minute;
  2. ask a person to move from armchair 77 to armchair 66, it takes 11 minute;
  3. 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:

  1. ask a person to move from armchair 11 to armchair 44, it takes 33 minutes;
  2. ask a person to move from armchair 22 to armchair 66, it takes 44 minutes;
  3. ask a person to move from armchair 44 to armchair 55, it takes 11 minute;
  4. 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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值