Codeforces 893d-credit Card (greedy)

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.