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. .NET-ORM框架EF-Code First代码优先

    前言 Code First顾名思义,通告代码创建实体与数据库.示例中我们会创建表,分表是Studen,Teacher. Code First实战示例 打开VS2013,创建一个项目我这里是用的MVC框 ...

  2. EF Code First 导航属性 与外键(转载)

    EF Code First 导航属性 与外键 一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就 ...

  3. 21.QT-QTreeWidget,QTabWidget

    QTreeWidget树形列表 设置标签相关函数 void QTreeWidget::setHeaderItem (QTreeWidgetItem * item ); void QTreeWidget ...

  4. Java实现Base64加密

    阅读本文约“2.5分钟” Java加解密系列,介绍Java加密解密的基础知识,并使用Base64算法实现加解密. 对于加密在企业中是非常常见的,就如邮件的传输,每个企业都会有自己设置安全方式,设置加密 ...

  5. Netty实战五之ByteBuf

    网络数据的基本单位总是字节,Java NIO 提供了ByteBuffer作为它的字节容器,但是其过于复杂且繁琐. Netty的ByteBuffer替代品是ByteBuf,一个强大的实现,即解决了JDK ...

  6. Reactor模式理解

    Reactor模式 也可以叫反应器模式或者应答者模式 reactor模式简介 让我们先了解一下阻塞I/O与非阻塞I/O I/O 是非常缓慢的 I/O绝对是计算机操作中最慢的.访问RAM的事件为ns级别 ...

  7. RNP项目遇到的坑

    1.nginx问题 和前端约定了在header中存放登录态k-v,选择的key是带下划线的. nginx 默认会丢弃带下划线的 header. 设置 underscores_in_headers on ...

  8. JavaScript易错知识点整理[转]

    前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函数参数,闭包问题及对象拷贝与赋值这6个方面进行由浅入深的介绍和讲解,其中也涉及了一 ...

  9. clipboard.js -- js实现将文本复制到剪贴板的方法

    资源 推荐使用:clipboard.js 官方教程地址:https://clipboardjs.com/#example-text 官方github地址:https://github.com/zeno ...

  10. Java map 详解

    Map 提供了一个更通用的元素存储方法.Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值. 初始化一个集合:  Map<String, String> map = ...