【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;
}

转载于:https://www.cnblogs.com/cjyyb/p/9687124.html

【BZOJ3872】Ant colony(二分,动态规划)相关推荐

  1. 【控制】蚁群算法(ACO,Ant Colony Optimization)及 Matlab 实现

    文章目录 简介 实现思路 规则 特点 应用 仿真 Ref. 简介 蚁群算法是一种用来寻找优化路径的概率型算法.它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过 ...

  2. 2019ICPC(徐州) - Colorful String(哈希+二分+动态规划/回文自动机)

    题目链接:点击查看 题目大意:给出一个字符串,询问该字符串中的所有回文子串中,各有多少不同的字母 题目分析:这个题题意很简单,在比赛的时候看到字符串第一反应是哈希,哈希+暴力+线段树果不其然的T掉了. ...

  3. 文献记录(part37)--A two-stage hybrid ant colony optimization for high-dimensional feature selection

    学习笔记,仅供参考,有错必纠 关键词:特征选择:蚁群优化:高维数据:分类:最佳特征子集大小 随便看看 文章目录 A two-stage hybrid ant colony optimization f ...

  4. 蚁群算法(Ant Colony Optimization)

    蚁群算法(Ant Colony Optimization) 蚁群算法简介 蚁群算法(Ant Clony Optimization, ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Age ...

  5. 【其他】结构技术优化算法--蚁群算法(ant colony optimization)

    目录 1 遗传基因算法 2 模拟退火算法 2.1爬山算法 2.2随机概率优化 3 群体智能算法 3.1蚁群算法 3.2粒子群算法 4总结 1 遗传基因算法 遗传算法(Genetic Algorithm ...

  6. 蚁群算法(Ant Colony Algorithm, ACA)简介及其MATLAB实现

    目录 算法概述 ACA算法的数学原理 算法步骤 ACA算法特点 补充:启发式算法 旅行商问题(TSP) ACA的MATLAB实现 算法概述 模拟蚂蚁觅食行为设计的算法.讲蚂蚁群觅食的特点抽象出来转化成 ...

  7. 路径算法:蚁群算法(ant colony optimization, ACO)

    一, 蚁群算法概述 自然界中有一个神奇的现象,即蚂蚁在没有提示的情况下总是能够找到从巢穴到食物的最短路径,这是为什么呢?原因就是蚂蚁在寻找食物时,能在其走过的路径上释放一种特殊的分泌物--信息素,随着 ...

  8. BZOJ 3872 ant colony

    一道比较有意思的好题目吧. 这道题其实思路应该是很有意思的. 我们注意到,这棵树被一条关键的边分成了两部分. 当从两边来的数量恰好是要求的数量时,才会计算答案. 那么我们考虑到题目中,传递信息的方式是 ...

  9. A hierarchical heterogeneous ant colony optimization based fingerprint recognition system

    摘要: 针对的问题: 1.当图像质量较差时,需要对指纹进行大量的预优化处理.这可能会引入错误的脊线图案,降低脊线图案的系统性能 2.指纹匹配算法的生物识别计算时间长,大型数据库上的指纹匹配可能效率低下 ...

最新文章

  1. cacti系列(一)之cacti的安装及配置监控mysql服务
  2. android常见错误-Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
  3. C# Activator
  4. linux-shell命令之chgrp(change group)【更改群组】
  5. 1356. 根据数字二进制下 1 的数目排序 golang
  6. ZooKeeper -- API文档
  7. 如何在 ES5 环境下实现一个const ?
  8. 虚拟机Class文件结构笔记
  9. Eclipse+Maven创建webapp项目一
  10. ipad的文件连接服务器,使用Termius从iPad连接到Linux服务器
  11. golang在windows下编译Linux下的文件
  12. SAP-PP后台配置(第二部分)
  13. 【React 】基于Antd Design的Switch开关选择器控件封装
  14. 小波包分解、重构 matlab代码
  15. win10系统电脑提示此程序被组策略阻止的解决办法
  16. 小技巧2:Python 实现阿拉伯数字转化为中文数字
  17. linux基因组文件,科学网-NGS基础 - 参考基因组和基因注释文件-陈同的博文
  18. 3D数学 - 三角函数
  19. nginx【nginx跨域、nginx开启gizp压缩、nginx服务器部署项目】
  20. 小白白红队初成长(1)被动信息收集

热门文章

  1. Patches Are All You Need?
  2. 计算机网络 ip协议是,IP协议是什么
  3. LPCSTR,LPCTSTR,LPCWSTR的区别
  4. IDEA插件-----FindBugs
  5. VS2019安装失败
  6. gmail 无法登录 原因解决
  7. 【导数术】1.导数基本公式
  8. 分布式系统关注点——360°的全方位监控
  9. 消防设施操作员考试真题、模拟练习题库(6)
  10. 2020-05-10 华为机考机试题目两题