POJ 3061 Subsequence(Two Pointers)

forever97 posted @ 2016年11月02日 00:26 in 算法-Two pointers with tags Two pointers , 793 阅读

 

【题目链接】 http://poj.org/problem?id=3061

 

【题目大意】

    给出S和一个长度为n的数列,问最短大于等于S的子区间的长度。

 

【题解】

    利用双指针获取每一个恰好大于等于S的子区间,更新答案即可。

 

【代码】

#include <cstdio>
int T,a[100005];
int main(){
    scanf("%d",&T);
    while(T--){
        int n,S,s,h,t,ans;
        scanf("%d%d",&n,&S);ans=n;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1,j=1;i<=n;i++){
            s+=a[i];
            if(s>=S){
                ans=min(ans,i-j);
                s-=a[j++];
            }
        }printf("%d\n",ans);
    }return 0;
}
  • 无匹配
pavzi.com 说:
2024年1月18日 16:40

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter