[bzoj4849][Neerc2016]Mole Tunnels

来自FallDream的博客,未经允许,请勿转载,谢谢



貌似是省队集训女队讲的题。。。

今天在bzoj找一道题无果,但是翻到了这道就顺便写了下。

鼹鼠们在底下开凿了n个洞,由n-1条隧道连接,对于任意的i>1,第i个洞都会和第i/2(取下整)个洞间有一条隧道,第i个洞内还有ci个食物能供最多ci只鼹鼠吃。一共有m只鼹鼠,第i只鼹鼠住在第pi个洞内,一天早晨,前k只鼹鼠醒来了,而后n-k只鼹鼠均在睡觉,前k只鼹鼠就开始觅食,最终他们都会到达某一个洞,使得所有洞的ci均大于等于该洞内醒着的鼹鼠个数,而且要求鼹鼠行动路径总长度最小。现对于所有的1<=k<=m,输出最小的鼹鼠行动路径的总长度,保证一定存在某种合法方案。

n<=10^5

看出是完全二叉树上费用流  建图比较简单 但是并不能过

发现树高是log级别的,所以考虑直接手动模拟。

f[x]表示从x走到子树中的一个可以觅食的点的最短距离,顺便记一下是哪个点。

一条边记一下它的正向通过次数和反向通过次数,如果他们相同那么两条边的距离都是1,不然有一个方向会因为反向边而变成-1

然后直接枚举lca更新答案,并且更新通过次数和f数组即可。

复杂度nlogn

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 100000
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,p[MN+5],tms[MN+5],f[MN+5],From[MN+5],ans=0;
inline int GetU(int x){return tms[x]<=0?1:-1;}
inline int GetD(int x){return tms[x]>=0?1:-1;}
void update(int x)
{
    int l=x<<1,r=x<<1|1;
    if(p[x]) f[x]=0,From[x]=x;
    else
    {
        f[x]=1e9,From[x]=0;
        if(l<=n&&f[l]+GetD(l)<f[x]) f[x]=f[l]+GetD(l),From[x]=From[l];
        if(r<=n&&f[r]+GetD(r)<f[x]) f[x]=f[r]+GetD(r),From[x]=From[r];
    }
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=n;++i) p[i]=read();
    for(int i=n;i;--i) update(i);
    for(int i=1;i<=m;++i)
    {
        int x=read(),mn=f[x],Best=x,d=0;
        for(int t=x;t>1;t>>=1)
            if(f[t>>1]+(d+=GetU(t))<mn)
                mn=f[t>>1]+d,Best=t>>1;
        printf("%d ",ans+=mn);--p[From[Best]];
        for(int i=From[Best];i>Best;i>>=1)
            ++tms[i],update(i);
        for(int i=x;i>Best;i>>=1)
            --tms[i],update(i);
        for(int i=Best;i;i>>=1) update(i);
    }
    return 0;
}
时间: 2025-01-05 20:06:23

[bzoj4849][Neerc2016]Mole Tunnels的相关文章

【BZOJ4849】[Neerc2016]Mole Tunnels 模拟费用流

[BZOJ4849][Neerc2016]Mole Tunnels Description 鼹鼠们在底下开凿了n个洞,由n-1条隧道连接,对于任意的i>1,第i个洞都会和第i/2(取下整)个洞间有一条隧道,第i个洞内还有ci个食物能供最多ci只鼹鼠吃.一共有m只鼹鼠,第i只鼹鼠住在第pi个洞内,一天早晨,前k只鼹鼠醒来了,而后n-k只鼹鼠均在睡觉,前k只鼹鼠就开始觅食,最终他们都会到达某一个洞,使得所有洞的ci均大于等于该洞内醒着的鼹鼠个数,而且要求鼹鼠行动路径总长度最小.现对于所有的1<=k

Codeforces Gym 101190M Mole Tunnels - 费用流

题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\right\rfloor$个巢穴连通.第$i$个巢穴在最终时允许$c_i$只醒来的鼹鼠最终停留在这.已知第$i$只鼹鼠在第$p_i$个巢穴睡觉.要求求出对于每个满足$1 \leqslant k \leqslant n$的$k$,如果前$k$只鼹鼠醒来,最小的移动距离的总和. 考虑费用流的建图和暴力做法,把原图的

Caves and Tunnels

Caves and Tunnels Time limit: 3.0 secondMemory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that there exist

Destroy Tunnels(矩阵水题)

Destroy Tunnels Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 39  Solved: 22 [Submit][Status][Web Board] Description Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely

Configure Open vSwitch Tunnels

转自: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux_OpenStack_Platform/4/html/Installation_and_Configuration_Guide/Configuring_Open_vSwitch_tunnels.html 13.4.4. Configure Open vSwitch Tunnels Tunneling with Open vSwitch allows

HDU 4856 Tunnels(bfs+状压dp)

题目大意:给你一个N*N的图让你到达所有的"."点,"#"不能通过,有m组每组有一个入口,一个出口,入口可以传送到出口,不知道经过m组的先后顺序,让你求出走过所有的"."的最小时间. 思路:先bfs出来所有的m之间的最短距离,然后dp[j][i] 表示,在j状态下开始第i步的最小路程,枚举找到一个最小的dp[1<<m - 1][i],就是最短距离,否则输出"-1". Tunnels Time Limit: 3000

HDU 4856 Tunnels

Tunnels Time Limit: 1500ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 485664-bit integer IO format: %I64d      Java class name: Main Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, t

HDU-4856 Tunnels (BFS+状压DP)

Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of cur

(状压dp)ABC 067 F : Mole and Abandoned Mine

Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1through N and M edges. The i-th edge connects Vertices ai and bi, and it costs ciyen (th