Superhero Battle

题目地址:

https://ac.nowcoder.com/acm/problem/113566

基本思路:

比较简单的数学+模拟,我们先考虑一下无限循环下去的情况,
很明显只要在一轮循环中不能让HP清零,并且每轮的总和是大于等于零的,那么就会无限循环下去;
否则我们实际上只需要枚举最后一轮怪物被杀的位置,然后直接用剩余的血量去计算一下之前循环的轮数就行了。
算出每个位置的结果取最小就是答案了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define debug(x) cerr << #x << " = " << x << '\n';
#define pll pair <ll, ll>
#define fir first
#define sec second
#define INF 0x3f3f3f3f
#define int ll
const int mod = (int)1e9 + 7;
void add(int &x, int y) { x += y; if(x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if(x < 0) x += mod; }

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}


const int maxn = 2e5 + 10;
int H,n,d[maxn],sum[maxn];
signed main() {
  IO;
  cin >> H >> n;
  rep(i,1,n) {
    cin >> d[i];
    sum[i] = sum[i - 1] + d[i];
  }
  if(sum[n] >= 0){
    int res = -1;
    for(int i = 0 ; i <= n ; i++){
      if(H + sum[i] <= 0) { res = i; break; }
    }
    cout << res << '\n';
    return 0;
  }
  int ans = (int)1e18;
  int tmp = -sum[n];
  for(int i = 0 ; i <= n ; i++){
    int now = H + sum[i];
    if(now <= 0){
      ans = min(ans,i);
      continue;
    }
    int need = (now + tmp - 1) / tmp;
    int res = i + need * n;
    ans = min(ans,res);
  }
  cout << ans << '\n';
  return 0;
}