hdu-7007-净化
第一轮跑完的值设为x
第二轮起点是x,如果加到某个数变成了负数,则后边部分和第一轮相同,不可能依靠每一轮加sum达到m,如果这种情况前两轮无法达到m,则不会到达m。
可以发现先跑前两轮判断是否存在解,因为第一轮最后正值+第二轮前边正值可能满足条件,如果不满足条件那么只能靠每轮 + sum 来达到m,所以sum<=0 时输出-1
mx = 第一轮结束的值 + 一轮中可以得到的最大值(作为最后一轮
所以mx也可以单独求,先跑第一轮得出x,然后维护一轮最大值作为最后一轮
中间缺的用sum补齐,向上取整
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 998244353;
ll n, m;
int a[maxn];
void work()
{
cin >> n >> m;
ll sum = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i];
ll x = 0, mx = 0, ans = -1;
for (int j = 1; j <= 2; j++)// 跑前两轮
{
for (int i = 1; i <= n; i++)
{
x += a[i];
if (x < 0) x = 0;
mx = max(mx, x);
if (x >= m)
{
cout << j << endl;return;
}
}
}
//cout << sum << " " << mx << " " << ans << endl;
if (sum > 0)
cout << 2 + (m - mx + sum - 1) / sum << endl;
// 2代表第一轮和最后一轮这两轮
else cout << -1 << endl;
}
int main()
{
int T;
cin>>T;
while(T--)
work();
return 0;
}
2710

被折叠的 条评论
为什么被折叠?



