【BZOJ3872】Ant colony(二分,动态规划)

题面

又是权限题。。。

Description

There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are g groups of m1,m2,...,mg ants respectively. These groups will enter the ant hill one after another, each successive group entering once there are no ants inside. Inside the hill, the ants explore it in the following way:

Upon entering a chamber with d outgoing corridors yet unexplored by the group, the group divides into d groups of equal size. Each newly created group follows one of the d corridors. If d=0, then the group exits the ant hill.

If the ants cannot divide into equal groups, then the stronger ants eat the weaker until a perfect division is possible. Note that such a division is always possible since eventually the number of ants drops down to zero. Nothing can stop the ants from allowing divisibility - in particular, an ant can eat itself, and the last one remaining will do so if the group is smaller than d.

The following figure depicts m ants upon entering a chamber with three outgoing unexplored corridors, dividing themselves into three (equal) groups of floor(m/3) ants each.

A hungry anteater dug into one of the corridors and can now eat all the ants passing through it. However, just like the ants, the anteater is very picky when it comes to numbers. It will devour a passing group if and only if it consists of exactly k ants. We want to know how many ants the anteater will eat.

给定一棵有n个节点的树。在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁。这些蚂蚁会相继进入树中,而且要保证每一时刻每个节点最多只有一群蚂蚁。这些蚂蚁会按以下方式前进:

·在即将离开某个度数为d+1的点时,该群蚂蚁有d个方向还没有走过,这群蚂蚁就会分裂成d群,每群数量都相等。如果d=0,那么蚂蚁会离开这棵树。

·如果蚂蚁不能等分,那么蚂蚁之间会互相吞噬,直到可以等分为止,即一群蚂蚁有m只,要分成d组,每组将会有floor(m/d)只,如下图。

一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为k只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。

Input

The first line of the standard input contains three integers n, g, k (2<=n,g<=1000000, 1<=k<=10^9), separated by single spaces. These specify the number of chambers, the number of ant groups and the number of ants the anteater devours at once. The chambers are numbered from 1 to n.

The second line contains g integers m[1],m[2],...,mg, separated by single spaces, where m[i] gives the number of ants in the i-th group at every entrance to the ant hill. The n-1 lines that follow describe the corridors within the ant hill; the i-th such line contains two integers a[i],b[i] (1<=a[i],b[i]<=n), separated by a single space, that indicate that the chambers no.a[i] and b[i] are linked by a corridor. The anteater has dug into the corridor that appears first on input.

第一行包含三个整数n,g,k,表示点数、蚂蚁群数以及k。

第二行包含g个整数m[1],m[2],...,m[g],表示每群蚂蚁中蚂蚁的数量。

接下来n-1行每行两个整数,表示一条边,食蚁兽埋伏在输入的第一条边上。

Output

Your program should print to the standard output a single line containing a single integer: the number of ants eaten by the anteater.

一个整数,即食蚁兽能吃掉的蚂蚁的数量。

Sample Input

7 5 3

3 4 1 9 11

1 2

1 4

4 3

4 5

4 6

6 7

Sample Output

21

题解

把树用第一条边拆成两半看,然后每个点都可以卡出一个数量的范围,递推下去,在每个叶子节点二分即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define inf 1ll*a[G]
#define MAX 1000100
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct Line{int v,next;}e[MAX<<1];
int h[MAX],cnt=1,dg[MAX];
inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;++dg[u];}
int n,U,V,G,K,a[MAX];
ll l[MAX],r[MAX],ans;
void dfs(int u,int ff)
{
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;if(v==ff)continue;
l[v]=min(inf+1,l[u]*(dg[u]-1));
r[v]=min(inf,(r[u]+1)*(dg[u]-1)-1);
dfs(v,u);
}
}
int find1(int x){return lower_bound(&a[1],&a[G+1],x)-a-1;}
int find2(int x){int p=upper_bound(&a[1],&a[G+1],x)-a;return a[p]==x?p:p-1;}
int main()
{
n=read();G=read();K=read();
for(int i=1;i<=G;++i)a[i]=read();
sort(&a[1],&a[G+1]);
for(int i=1;i<n;++i)
{
int u=read(),v=read();
Add(u,v);Add(v,u);
if(i==1)U=u,V=v;
}
l[U]=l[V]=r[U]=r[V]=K;
dfs(U,V);dfs(V,U);
for(int i=1;i<=n;++i)
if(dg[i]==1)
if(l[i]<=r[i])
ans+=1ll*K*(find2(r[i])-find1(l[i]));
printf("%lld\n",ans);
return 0;
}

【BZOJ3872】Ant colony(二分,动态规划)的更多相关文章

  1. $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$

    正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...

  2. bzoj 3872 [Poi2014]Ant colony——二分答案

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开 ...

  3. bzoj 3872 [ Poi 2014 ] Ant colony —— 二分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 从食蚁兽所在的边向叶节点推,会得到一个渐渐放大的取值区间,在叶子节点上二分有几群蚂蚁符 ...

  4. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  5. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  6. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

  7. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

  8. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. SonarQube6.7.4安装部署

    1.准备工作 https://www.sonarqube.org Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.比如p ...

  2. 20155333 《网络对抗》 Exp5 MSF基础应用

    20155333 <网络对抗> Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:攻击手段,是能使攻击武器(payl ...

  3. AngularJS+bootstrap-switch 实现开关控件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Hadoop开发第6期---HDFS的shell操作

    一.HDFS的shell命令简介 我们都知道HDFS 是存取数据的分布式文件系统,那么对HDFS 的操作,就是文件系统的基本操作,比如文件的创建.修改.删除.修改权限等,文件夹的创建.删除.重命名等. ...

  5. Bluedroid协议栈HCI线程分析

    蓝牙进程中有多个线程,其中HCI 线程是负责处理蓝牙主机端和控制器的数据处理和收发的工作. 本篇文章就是分析一下该线程的数据处理流程. 1.跟HCI相关的接口 首先看看hci的相关的接口:在hci_l ...

  6. TFS2012独占签出设置

    说明:TFS2012默认是可以多人签出同一个文件.如果要设为独占签出,请看下面操作步骤 1. 2. 3. 然后选择工作区---编辑---高级.最后如下图,在位置那里选择服务器. END

  7. 在Microsoft Dynamic 365/2016环境使用LinqPad查询数据(不使用linqpad Microsoft Dynamic 365 Driver)

    在Microsoft Dynamic 365/2016环境使用LinqPad查询数据 老规矩,先上效果图: 实体集合: 实体属性: 属性值:  查询出的结果可以导出的格式: 操作步骤: 1.下载Lin ...

  8. 从浏览器输入URL到显示页面到底发生了什么?

    首先说明一下,当系统本地缓存了你所请求的资源时,会直接把缓存内容解析并显示,而不会进行以下的一系列行为. 一.DNS域名解析 至今的计算机数量可谓是数不胜数,而它们的唯一识别身份就是ip地址.我们常说 ...

  9. Android 测试之Monkey

    一.什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进 ...

  10. 自动化部署-Jenkins+SVN+MSBuild 一些补充

    1.ftp的使用 系统管理->插件管理 安装插件Publish Over FTP 系统管理->系统设置 配置ftp参数 如下图 进入任务配置,添加构建后操作 在使用过程中还遇到一个本地防火 ...