牛客小白月賽4 H 相鄰的糖果 思維
阿新 • • 發佈:2018-08-25
bits 當前 cout true 要求 ret bitset iostream 不能
現在你可以執行以下操作:
·你可以選擇任意一個盒子,在選擇的盒子內吃掉一個糖果。
對你的要求如下:
·任何m個相鄰的盒子內糖果數量不能超過x個。
請問,實現要求的最少操作次數是多少?
鏈接:https://www.nowcoder.com/acm/contest/134/H
來源:牛客網
題目描述
有n個盒子擺成一排,每個盒子內都有ai個糖果。現在你可以執行以下操作:
·你可以選擇任意一個盒子,在選擇的盒子內吃掉一個糖果。
對你的要求如下:
·任何m個相鄰的盒子內糖果數量不能超過x個。
請問,實現要求的最少操作次數是多少?
輸入描述:
第一行三個數字n, m, x(2 ≤ n,m ≤ 106
,1 ≤ x ≤ 109
)。i
第二行n個數字(1 ≤ a
≤ 109
)。
輸出描述:
輸出一個操作數,代表實現要求的最少操作數。示例1
輸入
復制3 2 3 2 1 2
輸出
復制0
說明
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; }
牛客小白月賽4 H 相鄰的糖果 思維