• # 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 }```
• 相关阅读:
Asp.net core 2.0.1 Razor 的使用学习笔记（三）
Asp.net core 2.0.1 Razor 的使用学习笔记（二）
Asp.net core 2.1 Razor 的使用学习笔记（一）
免费ssl证书申请和在IIS上启用https的使用教程
ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用（四）—用户注册
花生日记,网赚兼职____宝妈邀您加入精英团队
excel中如何将时间戳转换为日期格式
laravel 5.5 oauth2.0 跨域问题解决方案
在自己的linux服务器上面部署ShowDoc
Linux 环境下安装 Nginx+php+mysql 开发环境
• 原文地址：https://www.cnblogs.com/z-712/p/15079598.html