zoukankan      html  css  js  c++  java
  • POJ-3061 前缀和+二分搜索 模板题

    Subsequence

    原题链接:http://poj.org/problem?id=3061

    Description

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

    Input

    The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

    Output

    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3

    题解:一开始想用前缀和+双指针进行搜索,超时,巧用lower_bound()来搜索结果,真的很nice

     1 #include <string.h>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int maxn=1e5+5;
     6 int B[maxn];
     7 int main(){
     8     int N;
     9     scanf("%d",&N);
    10     int n,s,tmp=0;
    11     while(N--){
    12         memset(B,0,sizeof(B));
    13         scanf("%d%d",&n,&s);
    14         for(int i=1;i<=n;i++){
    15             scanf("%d",&tmp);
    16             if(i==1) B[i]=tmp;
    17             B[i]=B[i-1]+tmp;
    18         }
    19         int j=0,summ=0,minL=maxn;
    20         if (B[n]<s){
    21             printf("%d
    ",0);
    22             continue;
    23         } 
    24         if(B[1]>=s){
    25             printf("%d
    ",1);
    26             continue;
    27         }
    28         for(int i=1;B[i]+s<=B[n];i++){
    29             j=lower_bound(B+i+1,B+n+1,B[i]+s)-B;
    30             minL=min(minL,j-i);
    31         }
    32         printf("%d
    ",minL);
    33     }
    34     return 0;
    35 }

    菠萝写的更快地版本

     1 #include <string.h>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int maxn=1e5+5;
     6 int nums[maxn];
     7 int main(){
     8     int N;
     9     scanf("%d",&N);
    10     int n,s,tmp=0;
    11     while(N--){
    12         scanf("%d%d",&n,&s);
    13         memset(nums,0,sizeof(nums));
    14         for(int i=0;i<n;i++) scanf("%d",&nums[i]);
    15         int sums=0,j=0,flag0=0,ans=0;
    16         for (int i=0;i<n;i++){
    17             int flag=1;
    18             sums+=nums[i];
    19             while (sums>=s){
    20                 ans=i-j+1;
    21                 sums-=nums[j];
    22                 j+=1;
    23                 flag=0;
    24                 flag0=1;
    25             }
    26             if (flag && flag0){
    27                 sums-=nums[j];
    28                 j+=1;
    29             }
    30         }
    31         printf("%d
    ", ans);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    百练 1936 ll in All 解题报告
    百练 2804 词典 解题报告
    POJ 1226 Substrings 解题报告
    百练 2797 最短前缀 解题报告
    百练 2743 字符串判等 解题报告
    java创建线程的两种方式
    使用.Net Remoting传送Image对象
    Links [IronPython Workflow WCF]
    Hello World!
    Links [ .Net 3.0 Atlas ]
  • 原文地址:https://www.cnblogs.com/z-712/p/15079598.html
Copyright © 2011-2022 走看看