Codeforces - 102222H - Fight Against Monsters - 贪心
https://codeforc.es/gym/102222/problem/H
题意:有一堆怪兽,怪兽有HP和ATK。你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽。打的伤害递增,第一次1,第二次2,以此类推。
为什么感觉是贪心呢?证明一波。
首先开始打一个怪兽肯定一直打到死为止。那么打死他要求的次数可以二分出来(其实暴力也可以)。两只怪兽交换打的顺序会不会变好?
先打第一只怪兽:
\(num_1*sumatk+num_2*(sumatk-atk_1)\)
先打第二只怪兽:
\(num_2*sumatk+num_1*(sumatk-atk_2)\)
那么什么情况下先打第二只会更好呢?当然是上式大于下式:
\(num_1*sumatk+num_2*(sumatk-atk_1)>num_2*sumatk+num_1*(sumatk-atk_2)\)
化简:
\(num_1*atk_2>num_2*atk_1\)
当上式成立时需要交换。
插进优先队列里面一搞,又不会爆longlong。一波搞定。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read() {
int x=0;
int f=0;
char c;
do {
c=getchar();
if(c=='-')
f=1;
} while(c<'0'||c>'9');
do {
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
} while(c>='0'&&c<='9');
return f?-x:x;
}
inline void _write(int x) {
if(x>9)
_write(x/10);
putchar(x%10+'0');
}
inline void write(int x) {
if(x<0) {
putchar('-');
x=-x;
}
_write(x);
putchar('\n');
}
void TestCase(int ti);
int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int T=read();
for(int ti=1;ti<=T;ti++)
TestCase(ti);
}
/*--- ---*/
inline int s1(int x){
return ((x+1)*x)>>1;
}
struct Monster{
int hp;
int atk;
int num;
inline void calc(){
int l=1,r=1000,m;
while(1){
m=l+r>>1;
if(m==l){
int s=s1(l);
if(s>=hp){
num=l;
return;
}
else{
num=r;
return;
}
}
int s=s1(m);
if(s==hp){
num=m;
return;
}
else if(s<hp)
l=m+1;
else
r=m;
}
}
inline bool operator<(const Monster &m)const{
return !(atk*m.num>=m.atk*num);
}
}monster;
priority_queue<Monster> pq;
void TestCase(int ti) {
ll sumatk=0;
int n=read();
for(int i=1;i<=n;i++){
monster.hp=read();
monster.atk=read();
sumatk+=monster.atk;
monster.calc();
pq.push(monster);
}
ll sumdamage=0;
while(!pq.empty()){
monster=pq.top();
pq.pop();
sumdamage+=sumatk*monster.num;
sumatk-=monster.atk;
}
printf("Case #%d: %lld\n",ti,sumdamage);
}
Codeforces - 102222H - Fight Against Monsters - 贪心的更多相关文章
- Codeforces 1296D - Fight with Monsters
题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还 ...
- Codeforces Round #617 (Div. 3) D. Fight with Monsters
D : Fight with Monsters 题目大意 : 有一组数,每个值对应着一个怪物的 hp 值,现在有两个人,一个自己一个对手,每个人有一个攻击值, 两个人轮流攻击怪物,如果是自己将怪物先打 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- D. Fight with Monsters
D. Fight with Monsters time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)
There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health ...
- Fight Against Monsters Gym - 102222H【贪心】
贪心的策略 #include <bits/stdc++.h> using namespace std; ; typedef long long ll; struct m { int hp, ...
- 2018 ACM-ICPC 宁夏 H.Fight Against Monsters(贪心)
It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Eg ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
随机推荐
- 数据分析第二篇:matplotlib 常用的几个绘图方法
Matplotlib matplotlib是python的绘图库,使用它可以很方便的绘制出版质量级别的图形 matplotlib的基本功能 1.基本绘图 1.1 绘制坐标系中连续的线,设置线型/线宽/ ...
- 程序以html形式发送邮件注意问题
1.样式要写在内部,不要单独定义样式 2.div 使用背景图片在有些浏览器中出不来这时候需要用: <table style="width: 640px" cellpaddin ...
- Python—is和==
首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识).type(数据类型)和value(值). ==是python运算符中的比较操作符,用来比较判断两个对象的value(值)是否相 ...
- Python调试时__name__ =='__main__'的妙用
# python 文件自己运行,__name__的值就会等于__main__# 别的文件导入执行时,变成了导入文件的名字 例如: niuniu.py文件中,文件自己执行时,__name__的值就是__ ...
- 关于MFC视图文档框架的理解-1
一.单个文档和多个文档的区别: MFC分为对话框程序,单文档程序和多文档程序. 单个文档程序:一个主框架内仅允许打开一个视图,若要打开另一个,则当前的文档必须得关闭.单文档程序可以打开不同格式的文档. ...
- leetcode 226. Invert Binary Tree(递归)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- ROS 负载均衡
[xuan89@MikroTik] > :for i from=1 to=$z do= {/ip firewall mangle add action=mark-connection chain ...
- 不要使用Android Studio的Git Commit了---->记一次debug
今天下午写了一些代码,吃晚饭时分用Android Studio commit了一下,不知道有没有选择Commit and push,结果刚才代码出bug我想回滚到上个版本的时候,发现Android S ...
- ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- BZOJ5281:[Usaco2018 Open]Talent Show
我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...