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. SqlServer 技术点总结(持续更新)

    本文是用于记录自己平时遇到的一些SQL问题或知识点,以便以后自己查阅,会持续的更新,增加内容.发在博客园也可以和各位博友共同学习交流,如文中记录的有错误之处希望指出,谢谢. 一.用SQL语句调用作业 ...

  2. 再谈 C# 对象二进制序列化,序列化并进行 AES 加密

    对象的二进制序列化非常有用,也非常方便. 我们可以把对象序列化为字节数组,也可以把对象序列化到文件,还可以把对象序列化到文件并进行加密. 先引用这些命名空间: using System.IO;usin ...

  3. 线程 线程池 Task

    首先声明 这是读了 愉悦的绅士 文章 <菜鸟之旅——学习线程(线程和线程池)> <Task与线程> 的一些个人总结,还是那句话,如有不对,欢迎指正 文章以代码加注释的方法展示. ...

  4. java中获取路径的方法

    在class获取路径的方法,getResource有没有“\”的区别 System.out.println("" + this.getClass().getResource(&qu ...

  5. couldn't resolve host api.weixin.qq.com

    1.代理服务器突然出现 couldn't resolve host api.weixin.qq.com 不知原因 2.重启nginx无效-----代码肯定没有动过(之前出现过,过了一天恢复) 3.防火 ...

  6. JS使用cookie实现只出现一次的广告代码效果

    我们上网经常会遇到第一次需要登录而之后不用再登录的网站的情况,其实是运用了Cookie 存储 web 页面的用户信息,Cookie 以名/值对形式存储,当浏览器从服务器上请求 web 页面时, 属于该 ...

  7. vue-cli中安装方法

    源:http://www.cnblogs.com/jn1223/p/6656956.html vue-cli中安装方法   vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装 ...

  8. Keystone, Start, Failed to Load Bson

    If you have installed the Keystone.js, and properly installed mongodb, but when tried to start the k ...

  9. [Objective-C]用Block实现链式编程

    看这篇博客时最快让你上手ReactiveCocoa之基础篇看到作者介绍链式编程那一块,发现自己的钻研精神不足.想想自己使用链式编程也有段时间了,对,就是 Masonry 库.自己一直享受点语法带来的效 ...

  10. Anaconda管理Python环境

    Anaconda介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.Anaconda提供环境管理的功能,功能类似 Virtuale ...