1822. Hugo II's War

Time limit: 0.5 second

Memory limit: 64 MB
The glorious King Hugo II has declared a war—a war that is holy, victorious, almost bloodless, but ruinous!
Right after declaring the war the king has started summoning the army. He plans to send a recruitment order to all his immediate vassals, who will send the order to their vassals, and so on. In the
end, every nobleman of the kingdom will be involved in the preparation for the war.
As soon as a nobleman who has no vassals receives the order, he immediately begins recruiting soldiers and joins his overlord in a few days. If a nobleman has vassals, he waits until there are at least x%
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.
King Hugo II wants to state the number x in his recruitment order. The king needs as many soldiers as possible for his military campaign. However, if the recruitment takes more than t days,
the enemy may learn about the imminent intrusion and strike first. Help Hugo II find the maximal possible value of x.

Input

The first line contains the number n of noblemen in the kingdom and the maximal number of days tthat can be spent for summoning the army (1 ≤ n ≤ 104;
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

Output the maximal possible value of x with absolute error at most 10−4.

Sample

input output
6 3
1 2
2 2
2 1
1 2
1 4
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&#39;s War 树的结构+二分的更多相关文章

  1. BestCoder Round #65 HDOJ5592 ZYB&#39;s Premutation(树状数组+二分)

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. [杂题]URAL1822. Hugo II's War

    看懂题意的请直接跳过下一坨! 本人有表达障碍! ========================================== 题意: (题意真的很难很难懂啊!!!  去他娘的**) 有一个王国 ...

  3. 【bzoj2402】陶陶的难题II 分数规划+树链剖分+线段树+STL-vector+凸包+二分

    题目描述 输入 第一行包含一个正整数N,表示树中结点的个数.第二行包含N个正实数,第i个数表示xi (1<=xi<=10^5).第三行包含N个正实数,第i个数表示yi (1<=yi& ...

  4. 浅谈oracle树状结构层级查询测试数据

    浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...

  5. hdu4729 树链剖分+二分

    An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (J ...

  6. 【51nod1462】树据结构

    Source and Judge 51nod1462 Analysis 请先思考后再展开 dffxtz师兄出的题 做法一:暴力树剖+分块,时间复杂度为 $O(nlognsqrt n)$ 做法二:利用矩 ...

  7. 树状结构Java模型、层级关系Java模型、上下级关系Java模型与html页面展示

    树状结构Java模型.层级关系Java模型.上下级关系Java模型与html页面展示 一.业务原型:公司的组织结构.传销关系网 二.数据库模型 很简单,创建 id 与 pid 关系即可.(pid:pa ...

  8. 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

    大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...

  9. 由简入繁实现Jquery树状结构

    在项目中,我们经常会需要一些树状结构的样式来显示层级结构等,比如下图的样式,之前在学.net的时候可以直接拖个服务端控件过来直接使用非常方便.但是利用Jquery的一些插件,也是可以实现这些效果的,比 ...

随机推荐

  1. 请将你的App签名文件放进保险箱

    这是一篇以我自己的实际经历写的一篇文章. 当下移动开发正值火爆,由于门槛低,任何一个程序员都可以比较容易的进入移动开发领域,作为App或者游戏等移动开发者,当你开发完成一个作品后,准备上线时,需要对你 ...

  2. Angular——$http

    基本介绍 $http用于向服务端发起异步请求,同时还支持多种快捷方式如$http.get().$http.post().$http.jsonp.$hhtp也是属于内置服务的一种,这里特意提出来写一篇用 ...

  3. asp.net MVC 和 webForm的区别

    asp.net MVC请求过程 ASP.NET MVC框架只是给开发者提供了开发web应用程序的一种选择,并不是要取代Webform这两种技术各有优缺点,开发者需要根据实际情况,选择对应的技术有时候, ...

  4. android framework 图解分享

    android framework 图解 个人网站:http://www.51pansou.com Android视频下载:Android视频 Android源码下载:Android源码 Androi ...

  5. 【sqli-labs】 less63 GET -Challenge -Blind -130 queries allowed -Variation2 (GET型 挑战 盲注 只允许130次查询 变化2)

    引号闭合 http://192.168.136.128/sqli-labs-master/Less-63/?id=1' or '1'='1 剩下的和Less62一样

  6. mysqlworkbench 执行update语句报错:You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

    You are using safe update mode and you tried to update a table without a WHERE that uses a KEY colum ...

  7. 表格 —— 一个单元格插入多个tags

    <st #st [columns]="columns" [data]="data" [bordered]='true'> <ng-templa ...

  8. 简单的jsonp实现跨域原理

    什么原因使jsonp诞生?  传说,浏览器有一个很重要的安全限制,叫做"同源策略".同源是指,域名,协议,端口相同.举个例子,用一个浏览器分别打开了百度和谷歌页面,百度页面在执行脚 ...

  9. Plan & Future

    以下是OI省选前的数据结构与算法整理,可能还不是很全面.但是已经是全网相对比较全面的了.所有标记为“基础”“进阶”“中级”“提高”的知识为近些年来NOIp考察的内容,需重点掌握. 所有“高级”部分为N ...

  10. Linux基本文件类型

    Linux基本文件类型 基本文件类型 - : 普通文件 d : 文件目录 p : 管道文件 l : 软连接文件 c : 字符设备文件 s : socket文件