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.

Input

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.

Output

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.

Examples
input
1000 6
-100 -200 -300 125 77 -4
output
9
input
1000000000000 5
-1 0 0 0 0
output
4999999999996
input
10 4
-3 -6 5 4
output
-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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【CF1141E】Superhero Battle

    \[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]

  4. Codeforces1141E(E题)Superhero Battle

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn min ...

  5. E. Superhero Battle

    链接 [https://codeforces.com/contest/1141/problem/E] 题意 怪物开始的生命值,然后第i分钟生命值的变化 问什么时候怪物生命值为非正 分析 有一个巨大的坑 ...

  6. 【Codeforces 1141E】Superhero Battle

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分最后轮了几圈. 二分之后直接o(N)枚举具体要多少时间即可. 注意爆long long的情况. 可以用对数函数,算出来有多少个0 如果大于 ...

  7. Codeforces Round #547 (Div. 3) E. Superhero Battle (数学)

    题意:有一个HP为\(h\)的大怪兽,你需要轮流进行\(i\)次操作.每次可以使\(h+=d_i\)(\(d_i\)有正有负),当第\(n\)次操作完成后,再从第一次开始,问能否使得怪兽的HP变为\( ...

  8. Codeforces Round #547 (Div. 3) 题解

    Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:块级按钮(拉伸至父元素100%的宽度)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. luffy 那点事

    1 虚拟环境创建 2 后台:Django项目创建 3 后台配置 4 数据库配置 5 user模块User表 6 前台 7 前台配置 8 前端主页 9 后端主页模块设计 10 xadmin 后台管理 1 ...

  3. Percona XtraBackup不锁库搭建slave数据库-基于GTID

    Percona XtraBackup不锁库搭建slave数据库-基于GTID 1.下载安装epel源并安装 wget http://ftp.cuhk.edu.hk/pub/linux/fedora-e ...

  4. CXL联盟正式成立:成员均是行业巨头

    导读 今天,阿里巴巴.思科.戴尔EMC.Facebook.Google.HPE.华为.Intel.微软(按英文首字母排序)联合宣布,CXL联盟(Compute Express Link Consort ...

  5. Vue.js 内联样式绑定style

    html <div class="Menu" v-bind:style="{height:clientHeight}"> </div> ...

  6. 引用类型--Date类型

    要创建一个日期对象,使用new操作符和Date构造函数即可. var now = new Date() 在调用Date构造函数而不传递参数的情况下,新创建的对象自动获得当前日期和时间.如果想根据特定的 ...

  7. 总结了一下 Vue.nextTick() 的原理和用途

    对于 Vue.nextTick 方法,自己有些疑惑.在查询了各种资料后,总结了一下其原理和用途,如有错误,请不吝赐教. 概览 官方文档说明: 用法: 在下次 DOM 更新循环结束之后执行延迟回调.在修 ...

  8. 前端学习笔记系列一:3 Vue中的nextTick

    一.示例 先来一个示例了解下关于Vue中的DOM更新以及nextTick的作用. 模板 <div class="app"> <div ref="msgD ...

  9. Unity ShaderLab 学习笔记(一)

    因为项目的问题,有个效果在iOS上面无法实现出来- 因为shader用的HardSurface的,在android上面跑起来没有问题- 以为在iOS上也不会有问题,但是悲剧啊,技能效果一片漆黑- 而且 ...

  10. centos 搭建 git 服务端和客户端

    centos 搭建git需要设置远程服务端和客户端.远程代码存放在服务端,多个客户端可以共享和维护服务端代码. 一.服务端主机 1.创建ssh,大部分默认已经安装,有ssh就跳过 yum instal ...