URAL 1822. Hugo II's War 树的结构+二分
1822. Hugo II's War
Memory limit: 64 MB
end, every nobleman of the kingdom will be involved in the preparation for the war.
of his immediate vassals ready for the war, then summons his own troops and also joins his overlord. The glorious King Hugo II will go the war as soon as at least x% of his immediate vassals are ready.
the enemy may learn about the imminent intrusion and strike first. Help Hugo II find the maximal possible value of x.
Input
0 ≤ t ≤ 106). The noblemen are numbered from 1 to n. King Hugo II has number 1, and the noblemen with numbers from 2 to n are described in the following n −
1 lines. The i-th of these lines describes the nobleman with number i and contains integers pi and ti,
where pi is the number of his overlord and ti is the number of days thei-th
nobleman will need to summon his troops (1 ≤ pi ≤ n; 0 ≤ ti ≤ 100).
Output
Sample
| input | output |
|---|---|
6 3 |
50.00000000 |
题意:
输入 n 个人,3是表示有多少时间让编号1 的人召集人马。
然后按2到n 的编号 输入pi 和ti。各自是那个人直接 领主。和召集自己本部的人所需的时间。 编号1 是大领主 。
如果 每一个领主都仅仅能在 直属下属 有≥x% 的人召齐了人马才干够開始招自己的人。
问x最大是多少,能够让大领主在 t 的时间内 召齐自己的人出发打仗。
做法:
二分x,推断下这个x是否能在所需时间内召齐人马。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <vector> long long p[10010],t[10010];
long long nowt[10010];
long long pai[100010];
double x;
long long sum_t;
vector <long long >son[10010]; void dfs(long long nod)
{
if(son[nod].size()==0)
{
nowt[nod]=t[nod];
return ;
}
for(long long i=0;i<son[nod].size();i++)
dfs(son[nod][i]); long long need=ceil(1.0*son[nod].size()*x/100.0);//找到所需的最少人数是多少
for(long long i=0;i<son[nod].size();i++)
pai[i]=nowt[son[nod][i]]; sort(pai,pai+son[nod].size()); long long ret;
if(need==0)
ret=0;
else
ret=pai[need-1]; //手下的人中,花费最长时间的那个人 nowt[nod]=ret+t[nod];
} long long deal()
{
dfs(1);
if(nowt[1]>sum_t)
return 0;
else
return 1; }
int main()
{
long long n;
while(scanf("%I64d%I64d",&n,&sum_t)!=EOF)
{
for(long long i=1;i<=n;i++)
son[i].clear();
for(long long i=2;i<=n;i++)
{
scanf("%I64d%I64d",p+i,t+i);
son[p[i]].push_back(i);
} double l=0,r=100;//
double mid;
while(abs(r-l)>0.0001)
{
mid=(l+r)/2.0;
x=mid;
if(deal()==0)//所需时间大于 sum_t
r=mid;
else
l=mid;
}
printf("%.7lf\n",l);
}
return 0;
}
URAL 1822. Hugo II's War 树的结构+二分的更多相关文章
- BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- [杂题]URAL1822. Hugo II's War
看懂题意的请直接跳过下一坨! 本人有表达障碍! ========================================== 题意: (题意真的很难很难懂啊!!! 去他娘的**) 有一个王国 ...
- 【bzoj2402】陶陶的难题II 分数规划+树链剖分+线段树+STL-vector+凸包+二分
题目描述 输入 第一行包含一个正整数N,表示树中结点的个数.第二行包含N个正实数,第i个数表示xi (1<=xi<=10^5).第三行包含N个正实数,第i个数表示yi (1<=yi& ...
- 浅谈oracle树状结构层级查询测试数据
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- hdu4729 树链剖分+二分
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- 【51nod1462】树据结构
Source and Judge 51nod1462 Analysis 请先思考后再展开 dffxtz师兄出的题 做法一:暴力树剖+分块,时间复杂度为 $O(nlognsqrt n)$ 做法二:利用矩 ...
- 树状结构Java模型、层级关系Java模型、上下级关系Java模型与html页面展示
树状结构Java模型.层级关系Java模型.上下级关系Java模型与html页面展示 一.业务原型:公司的组织结构.传销关系网 二.数据库模型 很简单,创建 id 与 pid 关系即可.(pid:pa ...
- 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限
大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...
- 由简入繁实现Jquery树状结构
在项目中,我们经常会需要一些树状结构的样式来显示层级结构等,比如下图的样式,之前在学.net的时候可以直接拖个服务端控件过来直接使用非常方便.但是利用Jquery的一些插件,也是可以实现这些效果的,比 ...
随机推荐
- HTML——meta
http://www.cnblogs.com/jr1993/p/4542862.html
- HanLP自然语言处理包开源(包含源码)
支持中文分词(N-最短路分词.CRF分词.索引分词.用户自定义词典.词性标注),命名实体识别(中国人名.音译人名.日本人名.地名.实体机构名识别),关键词提取,自动摘要,短语提取,拼音转换,简繁转换, ...
- ASP MVC
V-view 显示层 C-controller 控制层 M-model 模型 D-database 数据库 S-Service 服务 D-Database/Dao 数据库/访问数据库的方法 View即 ...
- Application received signal SIGSEGV
Application received signal SIGSEGV (null) (( 0 CoreFoundation 0x0000000181037d50 <redacted> + ...
- Protecting resources in iPhone and iPad apps
源码:https://github.com/lingzhao/EncryptedResourceDemo UPDATE: The example project has been updated to ...
- RabbitMQ系列(五)--高级特性
在上一篇文章讲解MQ消息可靠性投递和幂等性中有提到confirm机制的重要性,现在更相信的说明一下 一.Confirm机制 Confirm就是消息确认,当Producer发送消息,如果Broker收到 ...
- java string与byte互转
1.string 转 byte[]byte[] midbytes=isoString.getBytes("UTF8");//为UTF8编码byte[] isoret = srt2. ...
- UVA - 12113 Overlapping Squares(dfs+回溯)
题目: 给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出这样的形状. 思路: dfs纸的张数,每一张中枚举这张纸左上角这个点的位置,暴力解题就可以了. 这个题的覆盖太 ...
- jdk版本特性
https://segmentfault.com/a/1190000004419611 java5 泛型 枚举 装箱拆箱 变长参数 注解 foreach循环 静态导入 格式化 线程框架/数据结构 Ar ...
- PAT 1079. 延迟的回文数
PAT 1079. 延迟的回文数 给定一个 k+1 位的正整数 N,写成 ak...a1a0 的形式,其中对所有 i 有 0 <= ai < 10 且 ak > 0.N 被称为一个回 ...