বুধবার, ১৬ আগস্ট, ২০১৭

UVA 1121 - Subsequence

#include <bits/stdc++.h>
using namespace std;
main()
{
    long long n,s;
    while(cin>>n>>s)
    {
        long long ar[100010]= {0},high=0,low=0,ans=n+1,i,sum=0,temp=0;
        for(i=0; i<n; i++)
        {
            cin>>ar[i];
        }
        sum=ar[0];
        while(high<n)
        {
            if(sum<s)
            {
                high++;
                if(high<n)
                {
                    sum+=ar[high];
                }
            }
            if(sum>=s)
            {
                temp=high-low+1;
                if(ans>temp)
                {
                    ans=temp;
                }
            }
            if(sum>=s&&high>low)
            {
                sum-=ar[low];
                low++;
            }
        }
        if(ans==n+1)
        {
            cout<<0<<endl;
        }
        else
            cout<<ans<<endl;
    }
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...