Transmission Door : Codeforces 893D
The main effect of the topic :
There is a credit card with an initial amount of 0. Every day there are turnover a[i], every morning you can go to the bank to replenish any amount (positive) money, each night need to do the following:
1. Money deposited into a credit card at the time of A[i]>0 A[i]
2. When a[i]<0, remove a[i from credit card
3. When a[i]=0, check the money in the credit card
The amount of money in the credit card must not exceed the upper limit D, if must exceed the upper limit D output-1. The money in the bank card must not be negative for each check and ask at least how many times it needs to be added to the bank.
idea :
Consider the output-1 situation, that is, to meet the minimum requirements, if the money in the credit card will be more than D, then output-1. Note that the money in the credit card must not be negative for each check, so if the money in the credit card is negative for each check, it is 0.
Then consider going to the bank at least several times to add money. Obviously, the more money in a bank card, the less it needs to be replenished, up to a maximum of D. But there is a new problem: if the current credit card has a D, and the next volume is positive, it will exceed the limit, not the question. So, if the money in the credit card is greater than D, you should put the money D.
Code :
#include <stdio.h>
#include <string.h>
int main ()
{
int i,n,d,f,sum,ans,a[100010];
while (~SCANF ("%d%d", &n,&d))
{
f=sum=0;
for (i=0;i<n;i++)
{//Enter turnover and judge whether it will exceed
the upper bound scanf ("%d", &a[i]);
Sum+=a[i];
If the money is less than 0, then 0 if
(a[i]==0&&sum<0) sum=0;
if (sum>d) f=1; Exceeding the upper limit
}
if (f) printf (" -1\n");
else
{
ans=sum=0;
for (i=0;i<n;i++)
{//ask for the least amount of additional money
sum+=a[i];
if (sum>d) Sum=d; If the current money is greater than the upper limit, set to D if
(a[i]==0)
{//Check if
(sum<0)
{//If the money is negative then result +1
ans++;
Sum=d; Add the money in the credit card to the maximum
}}
printf ("%d\n", ans)
;
}
return 0;
}