2020 Winter Holiday [gmoj2223] [hen hen laying eggs] [interval and]

Title description

The hens in the chicken country are the best at laying eggs. MGMG is a hen in the chicken country that has the highest egg production and is famous for the whole chicken country.
The n henhouses exclusively for laying eggs in the chicken country are lined up in the "laying center" of the chicken country, numbered from 1 to n from left to right. Each henhouse has a maximum egg laying capacity, and the i-th henhouse has a maximum egg laying capacity of ci. Sometimes the production of MGMG is too large to lay all the eggs in one henhouse, so I have to move to the next henhouse to continue laying eggs. Transfer until all the eggs are laid, or complain to the administrator of the "Egging Center" "The number of henhouses is too small, I can't fit enough eggs for one chicken!"
In order to save the physical energy spent during the transfer, please program to help MGMG find several consecutive chicken coops (as few as possible) so that it can lay all the eggs.

Input

Enter a total of 2 lines.
Enter two integers n and t in line 1 to indicate that there are n nests for laying eggs in the "laying center", and MGMG will lay t total eggs at a time.
In the second line, n positive integers ci (1≤i≤n), in turn indicate that the maximum number of eggs that can be laid in the i-th henhouse is ci.

Output

Output 1 line of an integer or a word. When outputting an integer, it means that at least several consecutive henhouses are required for MGMG to lay all the eggs. When MGMG ca n’t finish all the eggs after using all the henhouses, MGMG is very angry and outputs the word “Angry” (without double quotes, pay attention to capitalization).

Sample input

Input1:

5 4
1 2 1 2 3

Input2:

3 9
3 3 3

Input3:

3 5
1 2 1

Sample output

Output1:
2

Outupt2:
3

Output3:
Angry

Data range limitation

Insert picture description here

prompt

Sample1: In
sample 1, there are 5 henhouses, and the egg laying capacity is 1, 2, 1, 2, 3, respectively. MGMG If you choose henhouses 1, 2, and 3 to lay 4 eggs, but use 3 henhouses, and choose henhouses 4 and 5 can also lay 4 eggs (there are 1 more) Excess capacity), only 2 henhouses are used.
Note: Due to the discontinuous henhouses No. 2 and No. 4, it cannot be used as one of the options.

Sample2: In
sample 2, there are 3 henhouses, and the egg laying capacity is 3, 3, 3. MGMG can lay 3 eggs in each of these 3 consecutive henhouses, so that exactly 9 eggs are laid.

Sample3: In
sample 3, the total layable eggs of all henhouses are less than the MGMG's egg laying amount, which cannot meet the MGMG's egg laying demand, and outputs "Angry".

analysis

What do you think this question can't do?
In fact, I came up with a positive solution from the beginning, but only one detail was not made, and 70pts was gone.
This question is about prefixes and preprocessing, directly enumerating each interval, judging whether the interval sum is greater than t, and then choosing the smallest one in the competition.
Talk about why I started WA: because I did n’t consider that if there is one in the second loop, then the following are meaningless. Break it, otherwise TLE! !

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,t,a[1000010],ans,mx,mn=2147483647,sum;
int main()
{
    freopen("hen.in","r",stdin);
    freopen("hen.out","w",stdout);
    scanf("%d%d",&n,&t);
    for(register int i=1;i<=n;i++)
    {
    	scanf("%d",&a[i]);
    	sum+=a[i];
    	if(a[i]>=t)
    	{
    		cout<<1;
			return 0; 
		}
    	a[i]=a[i-1]+a[i];
	}
	if(sum<t)
	{
		cout<<"Angry";
		return 0;
	}
	for(register int i=1;i<=n-1;i++)
	{
		for(register int j=i+1;j<=n;j++)
		{
			int s;
			s=a[j]-a[i-1];
			if(s>=t)
			{
				mn=min(mn,j-i+1);
				break; 
			}
		}
	}
	printf("%d",mn);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · 8009 views

Guess you like

Origin blog.csdn.net/dglyr/article/details/105151038