CF1141E Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minutes. After a round ends, the next round starts immediately. This is repeated over and over again.
Each round has the same scenario. It is described by a sequence of n numbers: d1,d2,…,dn(−10^6≤di≤10^6). The i-th element means that monster's hp (hit points) changes by the value didi during the i-th minute of each round. Formally, if before the i-th minute of a round the monster's hp is h, then after the i-th minute it changes to h:=h+di
The monster's initial hp is H. It means that before the battle the monster has H hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 0. Print -1 if the battle continues infinitely.
The first line contains two integers H and n (1≤H≤10^12, 1≤n≤2⋅10^5). The second line contains the sequence of integers d1,d2,…,dn (−10^6≤di≤10^6), where di is the value to change monster's hp in the i-th minute of a round.
Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer k such that k is the first minute after which the monster is dead.
1000 6
-100 -200 -300 125 77 -4
9
1000000000000 5
-1 0 0 0 0
4999999999996
10 4
-3 -6 5 4
-1
题意解释:输入,给定了怪物的hp和n轮战斗对怪物造成的伤害。输出,怪物的hp降到0及以下即被击败输出第几分钟怪物被击败或-1.
解题思路:先判断第一个周期造成的最高伤害是多少和第一个周期是否对怪物造成了伤害,来确定怪物是否能被击败。然后我们通过计算求出到打败怪物的前一个周期的时间,再判断最后一个周期何时击败怪物。
其实可以说是个数学题,这里我用了二分来求解,但是long long精度不够,中间判断的时候将long long转为double来提高精度。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[];
int main()
{
ll hp,n;
scanf("%lld%lld%lld",&hp,&n,&a[]);
ll t=a[];
ll aa=;
ll mmin=min(t,aa);
ll flag=;
for(int i=;i<=n;++i)
{
scanf("%lld",&a[i]);
t+=a[i];
if(mmin>t)
{
mmin=t;
flag=i;
}
}
ll tmp=hp;
tmp+=mmin;
if(tmp<=)
{
for(int i=;i<=n;++i)
{
hp+=a[i];
if(hp<=)
{
cout<<i;
return ;
}
}
}
else
{
if(t>=)
{
cout<<-;
return ;
}
ll l=,r=;
while(l<=r)
{
double mid=(l+r)/;
if(hp+(mid*t)+mmin<=)
{
r=mid-;
}
else
{
l=mid+;
}
}
hp+=l*t;
for(int i=;i<=n;++i)
{
hp+=a[i];
if(hp<=)
{
cout<<i+l*n;
return ;
}
}
}
}
CF1141E Superhero Battle的更多相关文章
- E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题
E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #547 (Div. 3) E. Superhero Battle
E. Superhero Battle A superhero fights with a monster. The battle consists of rounds, each of which ...
- 【CF1141E】Superhero Battle
\[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]
- Codeforces1141E(E题)Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn min ...
- E. Superhero Battle
链接 [https://codeforces.com/contest/1141/problem/E] 题意 怪物开始的生命值,然后第i分钟生命值的变化 问什么时候怪物生命值为非正 分析 有一个巨大的坑 ...
- 【Codeforces 1141E】Superhero Battle
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分最后轮了几圈. 二分之后直接o(N)枚举具体要多少时间即可. 注意爆long long的情况. 可以用对数函数,算出来有多少个0 如果大于 ...
- Codeforces Round #547 (Div. 3) E. Superhero Battle (数学)
题意:有一个HP为\(h\)的大怪兽,你需要轮流进行\(i\)次操作.每次可以使\(h+=d_i\)(\(d_i\)有正有负),当第\(n\)次操作完成后,再从第一次开始,问能否使得怪兽的HP变为\( ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- Victor and String[Bestcoder #52 1004](回文树)
题目描述 Victor喜欢玩字符串.他认为一个字符串是迷人的,当且仅当字符串是回文的.Victor想玩n次.每次他都会做以下四种操作中的一种.操作1:在字符串的开头添加一个字符 c.操作2:在字符串的 ...
- 解题报告:luogu P1516 青蛙的约会
题目链接:P1516 青蛙的约会 考察拓欧与推式子\(qwq\). 题意翻译? 求满足 \[\begin{cases}md+x\equiv t\pmod{l}\\nd+y\equiv t\pmod{l ...
- Android中ListView结合CheckBox判断选中项
本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作.通过一个Demo来展示该功能,选中ListView ...
- Backbone.js 历史&文档
历史: 0.1.0版本产生于 ‘— Oct 13, 2010 — Docs’ 文档: https://www.html.cn/doc/backbone/#changelog
- CH9 顺序容器
本章主要介绍了标准库顺序容器,包括 顺序容器的公共接口,如构造函数,添加/删除操作等 利用迭代器访问容器 不同顺序容器的差异 string的特殊操作 容器适配器,如栈,队列等 9.1 “按字典序插入到 ...
- Task使用注意
1. CancellationTokenSource的使用 https://binary-studio.com/2015/10/23/task-cancellation-in-c-and-things ...
- Python实现的远程登录windows系统功能示例
https://www.jb51.net/article/142326.htm 重点是这几本书要好好读读!: 更多关于Python相关内容感兴趣的读者可查看本站专题:<Python进程与线程操作 ...
- Redis Cluster 获取主从关系
redis-cli -h 192.168.11.111 -p 6380 -c cluster slots | xargs -n8 | awk '{print $3":"$4&qu ...
- java文件的上传
文件的上传和下载在web应用中是非常常用,也是非常有用的功能. 例如:发送电子邮件时可以同过上传附件发送文件,OA系统中可以通过上传文件来提交公文,社交网站通过上传图片来自定义头像等等. 例如:下 ...
- java api 远程连接 hdfs
IDEA中新建Maven工程,添加POM依赖, 在IDE的提示中, 点击 Import Changes 等待自动下载完成相关的依赖包. <?xml version="1.0" ...