链接:https://www.nowcoder.com/acm/contest/134/H
来源:牛客网
第一行三个数字n, m, x(2 ≤ n,m ≤ 106
,1 ≤ x ≤ 109
)。i
第二行n个数字(1 ≤ a
≤ 109
)。
输出一个操作数,代表实现要求的最少操作数。
2 1 2满足题目要求,任意相邻的两个数值之和均不超过3,所以不需要进行任何操作。
分析:从左到右枚举每m个数的和sum,判断和与x的大小,如果大于x在当前的i盒糖果处吃掉sum-x颗糖果,同时跟新sum,ans
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll a[maxn];
int main() {
ios::sync_with_stdio(0);
ll n, m, x, ans = 0, sum = 0;
cin >> n >> m >> x;
for( ll i = 1; i <= n; i ++ ) {
cin >> a[i];
sum += a[i];
if( i > m ) {
sum -= a[i-m];
}
if( sum > x ) {
a[i] -= sum-x, ans += sum-x, sum = x;
}
}
cout << ans << endl;
return 0;
}
原文:https://www.cnblogs.com/l609929321/p/9532195.html