URAL 1822. Hugo II's War 树的结构+二分

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's War 树的结构+二分

时间: 2024-11-08 18:23:03

URAL 1822. Hugo II's War 树的结构+二分的相关文章

URAL 1822. Hugo II&amp;#39;s War 树的结构+二分

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

[杂题]URAL1822. Hugo II&#39;s War

看懂题意的请直接跳过下一坨! 本人有表达障碍! ========================================== 题意: (题意真的很难很难懂啊!!!  去他娘的**) 有一个王国,王国里有一个国王(编号为1),他有(编号为2~n) n-1个臣子(这些臣子并不全和他有直接关系) 然后呢 国王要去打架,但是只有当他的x%个及以上的直系下属(与他有直接关系的臣子)做好打架的准备了,他才能去打架 他的直系下属也有下属,也要其中x%及以上的下属做好打架准备了,那些直系下属才会开始准备

URAL 1707. Hypnotoad&#39;s Secret(树状数组)

URAL 1707. Hypnotoad's Secret 题目链接 题意:这题设置的恶心不能多说,构造点和矩形,大概就是问每个矩形里面是否包含点 思路:树状数组,把点排序,按y轴,在按x轴,在按询问,这样每次遇到一个点就在相应的扫描线上加,遇到查询就询问出左边到这个点位置的,就能预处理出每个点左下角包含的点的个数,然后每个矩形再利用容斥原理去搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm&

ural 1707. Hypnotoad&#39;s Secret(线段树)

题目链接:ural 1707. Hypnotoad's Secret 题目大意:给定N和M,然后N组s0, t0, Δs, Δt, k,每组可以计算出k个星星的坐标:M组a0, b0, c0, d0, Δa, Δb, Δc, Δd, q,每组要求算出q个矩形,判断矩形内是否包含星星,对于q≥20的情况要根据公式计算一个值即可. 解题思路:计算出所有的星星坐标和矩阵,这个每的说了,将矩阵差分成两点,通过计算出每个点左下角有多少个星 星,然后用容斥计算出矩阵内是否有点.这个属于线段树的一个应用. #

HDU4509-湫湫系列故事——减肥记II(线段树)

湫湫系列故事--减肥记II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2395    Accepted Submission(s): 1018 Problem Description 虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见- 但是没有什么能难倒高智商美女湫湫的,她决定另寻对策

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in co

ZOJ 3635 树状数组+二分

这题那时怎么想就是想不出来--而且今晚没有多大状态,自己都晕了--一题没做出来-- baoge解释好久才懂--唉--线段树,树状数组用得还是不够熟啊-- WA了二发,才知道二分错了,二分好久不用,老是出错了现在-- #include<iostream> #include<cstring> #include<string> #include<cstdio> #define sca(a) scanf("%d",&a) #define

HDU 2852 KiKi&#39;s K-Number (树状数组 &amp;&amp; 二分)

题意:给出对容器的总操作次数n, 接下来是这n个操作.这里对于一个容器提供三种操作, 分别是插入.删除和查找.输入0  e表示插入e.输入1  e表示删除e,若元素不存在输出No Elment!.输入2  e  k表示查找比e大且第k大的数, 若不存在则输出Not Find! 分析:这里考虑树状数组做的原因是在第三个操作的时候, 只要我们记录了元素的总数, 那通过求和操作, 便能够高效地知道到底有多少个数比现在求和的这个数要大, 例如 tot - sum(3)就能知道整个集合里面比3大的数到底有

树型结构

树型结构的基本概念 对大量的输入数据,链表的线性访问时间太慢,不宜使用.本文探讨另外一种重要的数据结构----树,其大部分时间可以保证操作的运行平均时间复杂度为O(logN),第一部分先来看一下树的一些预备知识. 首先看一下树形结构的样子,下图代表的是树型结构的一般形态: 由上图看得出树是一些节点的集合,总结一下树的一些基本概念: 1.结点:树中的数据元素都称之为结点 2.根:最上面的结点称之为根,一颗树只有一个根且由根发展而来,从另外一个角度来说,每个结点都可以认为是其子树的根 3.父亲:结点