E. Superhero Battle
time limit per test
2 seconds
memory limit per test
256 megabytes
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 kkis 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
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
const ll mod = 1000000007;
const int N = 5e5 + 5;
ll n, m;
ll d[200005];
ll sum1;
ll sum[200005];
long long Min=0;
long long H;
int main() {
cin>>H>>n;
for(int i=0;i<n;i++){
cin>>d[i];
sum1+=d[i];
sum[i]=sum1;
if(Min>sum1){
Min = sum1;
}
}
if((H>(-Min)&&sum1>=0)||Min>=0) {
cout<<-1; return 0;
}
if(H>(-Min)){
ll ro=H/-sum1;
ll le=H%-sum1;
while(le<=-Min){ // 最耗时的一部分
if(ro)le-=sum1;
ro--;
}
ll ans=ro*n+n;
le+=sum1;
for(int i=0; i<n; i++){
if(le+sum[i]<=0){
cout<<ans+i+1;
return 0;
}
}
}
else {
for(int i=0;i<n;i++){
if(sum[i]+H<=0){
cout<<i+1;
return 0;
}
}
}
return 0;
}
/* 下面是我的一个朋友的代码,改了最耗时的部分。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll arr[200010];
ll sum[200010];
int main(){
ll h,n;
cin>>h>>n;
cin>>arr[0];
sum[0]=arr[0];
for(int i=1;i<n;i++){
scanf("%I64d",&arr[i]);
sum[i]=sum[i-1]+arr[i];
}
ll mn=100000000;
for(int i=0;i<n;i++){
mn=min(mn,sum[i]);
}
if(sum[n-1]>=0&&h+mn>0){
printf("-1\n");
return 0;
}
ll ans=0;
if(h+mn>=0&&sum[n-1]!=0){
ans+=n*((h+mn)/(-sum[n-1]));
h+=((h+mn)/(-sum[n-1]))*sum[n-1];
}
if(h==0){
cout<<ans<<endl;
return 0;
}
for(int i=0;;i++){
ans++;
h+=arr[i%n];
if(h<=0){
break;
}
}
printf("%I64d\n",ans);
}
*/