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 ...
随机推荐
- java:Eclipse插件springsource-tool-suite的下载和安装
1.打开下载页面http://spring.io/tools/sts/all 找到这个,后补全部版本链接http://spring.io/tools/sts/legacy 插件压缩包下载安装: 链接下 ...
- idea中java异常
1. Compilation failed: internal java compiler error 解决方案:File-->Setting...-->Build,Execution,D ...
- Mysql备份和还原(命令)
1.备份方法一 ①.进入数据库 mysql -uroot -p pwd; ②.查看数据库 show databases; ③.备份数据库 mysqldump -hlocalhost -uroot(用户 ...
- IIS部署网站(建站)
双击IIS图标,运行IIS服务器: 如下图所示: 2 IIS打开后,选中网站,然后右键,在弹出的菜单中,选择新建,然后选择网站:如下图所hi: 3 在弹出的窗口中,直接点击下一步:如下图所示: 4 ...
- input 限制输入
只能输入数字 :<input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" /> 只 ...
- 【LeetCode】015 3Sum
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- IronPython 源码剖析系列(1):IronPython 编译器
自 IronPython 正式发布以来,由于对 Python 语言的喜爱所驱使,同时我想藉此去了解一下编程语言的编译器,分析器等程序是什么原理,如何运作的,所以我开始了对 IronPython 源代码 ...
- [转]CSS禁止文字选择
user-select有两个值: none:用户不能选择文本 text:用户可以选择文本 需要注意的是:user-select并不是一个W3C的CSS标准属性,浏览器支持的不完整,需要对每种浏览器进行 ...
- ssh免密码登录配置方法,(图示加命令)
首先,说明一下我们要做的是,serverA 服务器的 usera 用户免密码登录 serverB 服务器的 userb用户. 我们先使用usera 登录 serverA 服务器 [root@serve ...
- SQL中replace函数
string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...