(Jizhong) 2182. Goat graze (eat) and [prefix]

(File IO): input: eat.in output: eat.out
time limit: 1000 ms space constraints: 131072 KB specific restrictions
Goto ProblemSet


Title Description
After the lambs after class, have to graze on pasture. The village sheep now being featured demonstration village sheep examination, the leaders wanted to see the sheep pasture village.
Village sheep pasture is continuously distributed on each pasture has varying amounts of grazing sheep. The leaders want to see several consecutive pasture, but do not want to see more than T T sheep. The village leaders want to see more style village sheep, mostly to visit several pastures as far as possible.
Now, you decide to help the mayor with leaders to visit what some pastures, village leaders and to meet the requirements of.


Enter
the first line an integer N N and T T , indicates how many consecutive pasture sheep village shares, as well as leaders want to see the maximum number of sheep.
second line N N integers, between two integers separated by one space, the i-th ai expressed on the i-th pasture a i to sheep grazing. Number from 1 1 to N N

Output
output line, a total of two numbers, numbers indicate the start and end of the visit numbers, separated by a space. Always take direction from a number of small to large number. In addition, if the possibility of the same length, the smaller number of output starting point for the answer. Data ensure that at least there is an answer.


Sample input
. 5 10
. 6. 3. 1 2. 7

Sample Output
24


Data range limit
30 30 % of the data, 1 < = N < = 100 1<=N<=100 ;
60 60 % of the data, 1 < = N < = 1000 1<=N<=1000 ;
100 100 % of the data, 1 < = N < = 100000 , 0 < = a i < = 1 0 9 0 < = T < = 2 3 1 1 1<=N<=100000, 0<=ai<=10^9,0<=T<=2^31-1


Tips
to meet no more than the sum of consecutive 10 10 , there is 3 + 2 + 1 3+2+1 and 2 + 1 + 7 2+1+7 are two possibilities, priority output 2 2 to 4 4 Ge pasture.


Problem-solving ideas
do a prefix and then violence, combined with a little optimization. .


Code

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
long long n,t,a[100010],maxn,p,q,i;
int main(){
   freopen("eat.in","r",stdin);
   freopen("eat.out","w",stdout);
    scanf("%d%lld",&n,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        a[i]+=a[i-1];
    }
    i=0,maxn=0;
    while(i<n-maxn)
    {
        i++;
        for(int j=i+maxn;j<=n;j++)
        {
            if(a[j]-a[i-1]<=t)
            {
                maxn=max(maxn,j-i+1);
                p=i;
                q=j;
            }
            else
            break;
        }
    }
    printf("%d %d",p,q);
}
Published 119 original articles · won praise 8 · views 4900

Guess you like

Origin blog.csdn.net/kejin2019/article/details/105162552