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 ;
}

E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题的更多相关文章

  1. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

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

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

  3. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  4. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  5. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  6. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

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

  8. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

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

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

随机推荐

  1. 18.QT-QPlainEdit 信号与槽

    QPlainEdit编辑功能 Public Slots void appendHtml ( const QString & html ) void appendPlainText ( cons ...

  2. 博弈之——SG模板

    很久没搞博弈了.先来写个模板: 现在我们来研究一个看上去似乎更为一般的游戏:给定一个有向无环图和一个起始顶点上的一枚棋子,两名选手交替的将这枚棋子沿有向边进行移动,无法移动者判负.事实上,这个游戏可以 ...

  3. react学习笔记2

    1.build文件介绍 (1)react.js  是react的核心库 (2)react-dom.js  提供与DOM相关功能 (3)browser.js  是将JSX语法转为javascript语法 ...

  4. 实现DevOps需要的工具

    硬性要求:工具上的准备 代码管理(SCM):GitHub.GitLab.BitBucket.SubVersion 构建工具:Ant.Gradle.maven 自动部署:Capistrano.CodeD ...

  5. Link Between SAP SD, MM & FI

    Link Between SAP SD, MM & FI 1. In SAP you will always get integration with other modules. SD wi ...

  6. 23.Odoo产品分析 (三) – 人力资源板块(4) – 招聘流程(1)

    查看Odoo产品分析系列--目录 安装招聘流程模块:  可以看到我们在前面的章节中设置的"生产经理"岗位,和其他的看板视图一样,每一个岗位板块提供了各种便捷的操作入口和颜色设置. ...

  7. C# 利用ZXing.Net来生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...

  8. python协程--asyncio模块(基础并发测试)

    在高并发的场景下,python提供了一个多线程的模块threading,但似乎这个模块并不近人如意,原因在于cpython本身的全局解析锁(GIL)问题,在一段时间片内实际上的执行是单线程的.同时还存 ...

  9. mysql 获取一张表中, 另一张表不存在的数据

    编写sql语句中,经常需要编写获取一张数据表中不存在与另一张表的数据,相关编写方法如下: 方法1: 使用not in ,效率低下,在数据较小的情况下可以采用如下编写 SELECT * FROM a A ...

  10. 利用Selenium爬取淘宝商品信息

    一.  Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...