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;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：POJ - 3061 Subsequence 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)