HDU暑假多校第三场H.Monster Hunter
一、题意
给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP。因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物。现在,给定每只怪兽的a,b和树状结构的联通关系,求初始HP最小的击败顺序。
Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists of n intersections, labeled by 1,2,...,n, connected by n−1bidirectional roads. Little Q is now at the 1-th intersection, with X units of health point(HP).
There is a monster at each intersection except 1. When Little Q moves to the k-th intersection, he must battle with the monster at the k-th intersection. During the battle, he will lose ai units of HP. And when he finally beats the monster, he will be awarded bi units of HP. Note that when HP becomes negative(<0), the game will over, so never let this happen. There is no need to have a battle at the same intersection twice because monsters do not have extra life.
When all monsters are cleared, Little Q will win the game. Please write a program to compute the minimum initial HP that can lead to victory.
二、解题思路
首先考虑在一般情况下<仅仅给出每只怪物的A,B的情况下>如何进行选择。于是有:
- 如果两只怪物的b都大于a,则优先打击a更小的那个怪物。
- 如果一只怪物a<b,另一只a>b则优先打击a<b的。
- 如果两只怪物的b都小于a,于是有,最小血量的击败方案为min(max(ai,ai+aj-bi),max(aj,aj+ai-bj)),题解提到上述计算式实际上等于比较bi和bj
之后考虑,加入树边的情况。
考虑使用优先队列维护最优解,则
当某一个元素p为上述比较方案的最优解时,
且p的树根如果被选择后,选择p一定是最优解。
因此可以将p并入树根节点,后使p得所有子节点成为p的兄弟节点。——因为当p的父节点被选择后,p也已经默认被立刻选择了。
最终,当所有节点都是根节点的子节点时,计算并比较最优解的具体状态。
考虑一个细节:如何合并两个节点?
设ai,aj,bi,bj为两个节点的相关变量则有:
ak = min(max(ai,ai+aj-ai),max(aj,aj+ai-bj))
bk = ak + bi + bj - ai - aj
#include<bits/stdc++.h>
using namespace std; #define ll long long
const ll MAXN=;
#define veci vector<int> int fa[MAXN];
int cha[MAXN]; class Node
{
public:
ll a,b,num,ver;
Node(){}
Node(const Node &n)
{
this->a = n.a;
this->b = n.b;
this->num = n.num;
this->ver = n.ver;
}
bool operator < (Node const n)const{
ll tmpa = this->b - this->a;
ll tmpb = n.b - n.a;
if(tmpa >= && tmpb >=)return this->a > n.a;
if(tmpa >= && tmpb < )return false;
if(tmpa < && tmpb >=)return true;
if(tmpa < && tmpb < )return this->b < n.b;
}
};
Node nodes[MAXN];
int n;
veci G[MAXN]; void dfs(int now,int father)
{
fa[now] = father;
int len = G[now].size();
for(int i=;i<len;++i)
{
int tar = G[now][i];
if(tar == father)continue;
dfs(tar,now);
}
} int find_set(int tar)
{
if(cha[tar] == tar)return tar;
else return cha[tar] = find_set(cha[tar]);
} void init()
{
cin>>n;
for(int i=;i<n+;++i)
{
G[i].clear();
cha[i] = i;
}
priority_queue<Node> pq;
for(int i=;i<=n;++i)
{
cin>>nodes[i].a>>nodes[i].b;
nodes[i].num = i;
nodes[i].ver = ;
pq.push(nodes[i]);
}
for(int i=;i<n;++i)
{
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}dfs(,); ll ans = ;
ll money = ; while(!pq.empty())
{
Node node = pq.top();
pq.pop(); int now = node.num;
if(node.ver != nodes[now].ver)continue;
int father = fa[now];
father = find_set(father);
nodes[now].ver = -;
cha[now] = cha[father];
if(father == )
{
if(node.a > money)
{
ans += node.a-money;
money = node.b;
}else{
money -= node.a;
money += node.b;
}
continue;
} nodes[father].ver ++;
ll tmp = nodes[father].b +node.b - nodes[father].a-node.a;
nodes[father].a = max(nodes[father].a, nodes[father].a + node.a - nodes[father].b);
nodes[father].b = nodes[father].a + tmp;
Node newNode(nodes[father]);
pq.push(newNode);
} cout<<ans<<"\n";
} int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
while(t--)init(); return ;
}
HDU暑假多校第三场H.Monster Hunter的更多相关文章
- 2020牛客暑假多校训练营 第二场 H Happy Triangle set 线段树 分类讨论
LINK:Happy Triangle 这道题很容易. 容易想到 a+b<x a<x<b x<a<b 其中等于的情况在第一个和第三个之中判一下即可. 前面两个容易想到se ...
- HDU暑假多校第八场G-Card Game
一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...
- HDU暑假多校第八场J-Taotao Picks Apples
一.题意 给定一个序列,之后给出若干个修改,修改的内容为在原序列的基础上,将某一位元素的值改成给定的值<每次修改相互独立,不保存修改后的结果>.之后询问,在选择第一位元素的情况下,最长递增 ...
- HDU暑假多校第六场K-werewolf
一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...
- HDU暑假多校第四场J-Let Sudoku Rotate
一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...
- [题解]Magic Line-计算几何(2019牛客多校第三场H题)
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...
- 2019 牛客多校第三场 H Magic Line
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题目大意 给定 N 个不同的整数点,N 为偶数,求一条直线,这条直线能把这 N 个点对半分开,输出这条直线 ...
- 2019牛客多校第三场H Magic Line 思维
Magic Line 题意 给出n(偶)个整点 整点范围1000,找出一条直线,把n个点分成均等的两部分 分析 因为都是整数,并且范围比较小,所以直接按x排序找到在中间那一部分,并且把中间那一部分的点 ...
- [题解] 2019牛客暑期多校第三场H题 Magic Line
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...
随机推荐
- SpringMvc-helloword
说明:在此只说明helloword的简单实现,通过helloword例子先了解springMvc是这样工作的,然后在一步步的研究原理 配置web.xml 1.配置servlet servlet-cla ...
- 探索grep命令
grep是linux的一款搜索工具,基本啥linux版本都有自带此工具.下面部分参数若没有说明,表示功力不够,理解不了. 选择匹配器 -E 正则表达式(相当于egrep命令) -F 将pattern当 ...
- ZHS16GBK编码中汉字缺失
生产中使用ZHS16GBK编码的Oracle数据库,若出现字,则会出现乱码 原因是此字不存在在ZHS16GBK编码中 解决方法可以:此二字结构相同,但是后面的在ZHS16GBK编码中出现
- [EffectiveC++]item40:明智而审慎地使用多重继承
- Android(java)学习笔记56:Android InputMethodManager输入法简介
参见博客: http://blog.csdn.net/pi9nc/article/details/9196779
- 2019年5月训练记录(更新ing)
前言 \(ZJOI\)正式结束了. 但期中考试只考了年级\(216\),退役既视感... 于是就被抓回去补文化课了. 下半个学期可能要以文化课为主了吧! 但周三.周日应该还是会正常参加训练的,但其他时 ...
- 使用maven创建项目
http://192.168.4.112/rdmanager/main/index.jhtml 1.对于第一次下载某个项目的源码,按照下面的步骤进行: (1)在D:\projects\目录下的空白位置 ...
- html或jsp页面自动提交,无需每次重启服务
从eclipse转到idea遇到各种问题,之前eclipse可以自动保存页面内容无需重启服务,但是idea不可以,网上找了n种办法也没用,可能版本不一样吧,把我的解决方法纪录一下,方便以后有人遇到这个 ...
- 学大伟业 Day 3 培训总结
今天讲的字符串: 不多说,直接看题 一.表达式求值 题目大意: 输入一行一个表达式,计算其答案 表达式包含非负整数.加减乘除.括号 两种做法 ·栈 ·表达式树 这里更推荐表达式树,因为栈是先压进去,逆 ...
- Eclipse插件的卸载和安装
Eclipse 卸载插件: 右下角会有卸载进度 卸载完后 然后需要重启 Eclipse安装插件 选择本地下载好的插件 点击 Ok 插件下载地址:https://jaist.dl.sourceforge ...