cf1141 E. Superhero Battle(思维)
作者:互联网
题意:
在数组 a[]
生成的循环数组 \(a_{i+kn}=a_i\) 中,求最小的 \(j\) 使得 \(H+\sum_{i=1}^j a_i\le 0\)
思路:
这题很经典。
假设答案是 \(ans=kn+r\ \ (r<n)\),则应使 \(k\) 尽量小。维护一个前缀和最值即可。注意特判
二分找 k 也能过。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
ll H, a[N], n;
signed main()
{
scanf("%lld%lld", &H, &n);
ll maxx = -1e18;
for(int i = 1; i <= n; i++)
{ //为方便,把a变负
scanf("%lld", &a[i]), a[i] = -a[i];
a[i] += a[i-1], maxx = max(maxx, a[i]);
}
if(a[n] <= 0) //特判
{
for(int i = 1; i <= n; i++) if(a[i] >= H)
{
printf("%d", i);
return 0;
}
puts("-1"); return 0; //无解
}
ll k = max(0ll, (ll)ceil(1.0*(H - maxx) / a[n]));
ll ans = k * n, h = k * a[n];
for(int i = 0; i <= n; i++) if(h + a[i] >= H)
{
ans += i; break;
}
printf("%lld", ans);
return 0;
}
标签:Superhero,cf1141,int,ll,printf,ans,Battle,return,lld 来源: https://www.cnblogs.com/wushansinger/p/15700609.html