E. Superhero Battle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn 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 nn numbers: d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.

The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.

Input

The first line contains two integers HH and nn (1≤H≤10121≤H≤1012, 1≤n≤2⋅1051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106), where didi is the value to change monster's hp in the ii-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 kk such that kk is the first minute after which the monster is dead.

Examples
input

Copy
1000 6
-100 -200 -300 125 77 -4
output

Copy
9
input

Copy
1000000000000 5
-1 0 0 0 0
output

Copy
4999999999996
input

Copy
10 4
-3 -6 5 4
output

Copy
-1

这个题目不难,但是细节比较多,我觉得我做的很自闭,因为WA好多次,然后我就自闭了。
上网查了查题解,就是要考虑一次循环之后可以减少最多的值,这个要优先考虑,然后就是去找有多少个整次的循环。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
ll a[maxn]; int main()
{
int flag=,mark;
ll h, n,sum=,mi=inf;
cin >> h >> n;
ll g = h;
for(int i=;i<=n;i++)
{
scanf("%lld", &a[i]);
sum += a[i];
mi = min(mi, sum);
g += a[i];
if(g<=&&!flag)
{
mark = i;
flag = ;
}
}
if(flag)
{
printf("%d\n", mark);
return ;
}
if (sum >= )
{
printf("-1\n");
return ;
}
sum = -sum;
h += mi;
ll ans = h / sum * n;
ll cur = h % sum - mi;
while(cur>)
{
for(int i=;i<=n;i++)
{
cur += a[i];
ans++;
if (cur <= ) break;
}
//printf("cur=%d\n", cur);
}
printf("%lld\n", ans);
return ;
}

最新文章

  1. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
  2. fullpage.js全屏滚动插件使用小结
  3. git+github上传与管理
  4. unity3d 射弹基础案例代码分析
  5. linux 登录档配置分析
  6. flex安装时停在计算时间界面的解决办法
  7. CSS3学习之 transform 属性
  8. SQLSERVER 中GO的作用详解
  9. [LeetCode] Palindrome Partitioning II 解题笔记
  10. 『安全科普』WEB安全之渗透测试流程
  11. Modelsim中使用TCL脚本编写do文件实现自动化仿真
  12. Python爬虫入门教程 51-100 Python3爬虫通过m3u8文件下载ts视频-Python爬虫6操作
  13. python range的用法小题
  14. OO第二单元总结(多线程的电梯调度)
  15. SQL中的ALL,ANY,SOME的用法
  16. 洛谷 P1016 旅行家的预算
  17. THML分组元素
  18. mssql2012的分页查询
  19. ajax实现返回数据是html类型的跨域问题
  20. python json 数据操作

热门文章

  1. JSJ—类与对象
  2. Spring Boot(Spring的自动整合框架)
  3. &lt;a&gt;标签的特殊和文本的样式
  4. Ubuntu 安装 chrome
  5. 兼容浏览器的div透明
  6. 微信小程序使用wxParse,解决图片显示路径问题
  7. [总结]高效的jQuery代码编写技巧
  8. BZOJ1101: [POI2007]Zap(莫比乌斯反演)
  9. CSS超全笔记(适合新手入门)
  10. django rest framework 与 Vue 整合遇到的坑