【Educational Codeforces Round 33 D】Credit Card

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

每次遇到0的时候,看看当前累计的delta是多少. 如果大于0,则temp = d-delta; 小于0,取temp2 = min(d-max{delta}(这里max指之前一段0和0之间的区域),temp); 然后delta显然可以直接加上temp2,可以保证在0和0之间不会超过d. 然后看看delta是不是大于等于0了。 是的话,temp = delta,否则delta=inf,ans++; 然后不论如何delta都直接变成0,枚举下一段(每次都直接变成0,这样保证0与0之间的段的delta值尽可能小) 最后输出ans

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

int n,d,a[N+10];

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\c++source\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	cin >> n >> d;
	for (int i = 1;i <= n;i++)
		cin >> a[i];
	int now = 0;
	int ans = 0,ma = 0,temp = 0;
	for (int i = 1;i <= n;i++){
	 	if (a[i]==0){
	 	 	if (now < 0){
	 	 		int temp2 = min(temp,d - ma);
   	 		 	now+= temp2;

   	 		 	if (now >= 0){
  					temp = now;
   	 		 	}else{
   	 		 	 	temp = (int) 2e9;
   	 		 	 	ans++;
   	 		 	 	ma = 0;
   	 		 	}
   	 		 	now = 0;
	 	 	}else{
	 	 	 	temp = min(temp,d-now);
	 	 	 	ma = now + a[i+1];
	 	 	}
	 	}else{
	 	 	now += a[i];
	 	 	ma = max(ma,now);
	 	 	if (now > d){
	 	 	 	return cout << -1 << endl,0;
	 	 	}
	 	}
	}
	cout << ans << endl;
	return 0;
}	
原文地址:https://www.cnblogs.com/AWCXV/p/7888354.html