# POJ - 3061 Subsequence

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100005;

int A[MAXN],B[MAXN];
int n,S;

int main(){
int t;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&S);
for (int i = 1; i <= n; i++)
scanf("%d",&A[i]);
B[0] = 0;
for (int i = 1; i <= n; i++)
B[i] = B[i-1] + A[i];
int ans = n+1;
for (int j = 1; j <= n; j++){
int i = lower_bound(B,B+j,B[j]-S) - B;
if (i > 0)
ans = min(ans,j-i+1);
}
printf("%d\n",(ans == n+1) ? 0 : ans);
}
return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 100005;

int A[MAXN],B[MAXN];
int n,S;

int main(){
int t;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&S);
for (int i = 1; i <= n; i++)
scanf("%d",&A[i]);
B[0] = 0;
for (int i = 1; i <= n; i++)
B[i] = B[i-1] + A[i];
int i = 1,ans = n+1;
for (int j = 1; j <= n; j++){
if (B[i-1] > B[j] - S)
continue;
while (B[i] <= B[j] - S)
i++;
ans = min(ans,j-i+1);
}
printf("%d\n",(ans == n+1) ? 0 : ans);
}
return 0;
}

02-10 780

04-12 478

07-15 453

10-07 264

05-27 1064

11-03 3342

08-01 90

02-06 40

09-05 226

#### poj Common Subsequence c++

2008年12月15日 654B 下载