链接:https://www.nowcoder.com/acm/contest/134/H
来源:牛客网
题目描述
有n个盒子摆成一排,每个盒子内都有ai个糖果。
现在你可以执行以下操作:
·你可以选择任意一个盒子,在选择的盒子内吃掉一个糖果。
对你的要求如下:
·任何m个相邻的盒子内糖果数量不能超过x个。
请问,实现要求的最少操作次数是多少?
现在你可以执行以下操作:
·你可以选择任意一个盒子,在选择的盒子内吃掉一个糖果。
对你的要求如下:
·任何m个相邻的盒子内糖果数量不能超过x个。
请问,实现要求的最少操作次数是多少?
输入描述:
第一行三个数字n, m, x(2 ≤ n,m ≤ 106
,1 ≤ x ≤ 109
)。i
第二行n个数字(1 ≤ a
≤ 109
)。
输出描述:
输出一个操作数,代表实现要求的最少操作数。
示例1
说明
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; }