poj 3061 Subsequence【简单尺取】
2017-07-19 17:05
387 查看
Subsequence
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
Sample Output
思路:补一篇博客,二分也可以解决,尺取更快;
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15330 | Accepted: 6480 |
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
思路:补一篇博客,二分也可以解决,尺取更快;
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define max_n 100010 using namespace std; typedef long long LL; int s[max_n]; int main() { int n,T,m; scanf("%d",&T); while(T--) { int sum=0,l=0,r=0,ans=0x3f3f3f3f; scanf("%d %d",&n,&m); for(int i=0;i<n;i++) { scanf("%d",&s[i]); sum+=s[i]; } if(sum<m) { printf("0\n"); continue; } sum=0; // for(int i=0;i<n;i++)耗时一样两种方法 // { // sum+=s[i]; // r++; // if(sum>=m) // { // int j=l-1; // while(sum>=m) // { // j++; // sum-=s[j]; // } // l=j+1; // ans=min(ans,r-l+1); // } // } // printf("%d\n",ans); int res=n+1; int S=0,t=0; while(1) { while(t<n && sum<m) { sum+=s[t++]; } if(sum<m) break; res=min(res,t-S); sum-=s[S++]; } if(res>n) res=0; printf("%d\n",res); } return 0; }
相关文章推荐
- poj 3061 Subsequence 简单题。。可以用二分也可用尺取
- POJ 3061 Subsequence
- POJ-3061 Subsequence
- POJ 3061 Subsequence(尺取法)
- poj 3061 Subsequence(尺取法)
- poj-3061-Subsequence(二分思想)
- POJ 3061 Subsequence(尺取法)
- poj-3061-Subsequence & 尺取法
- POJ 3061 Subsequence【尺取法】
- POJ 3061 Subsequence
- POJ 3061 Subsequence ( 尺取法 )
- poj-3061-Subsequence
- POJ2533——Longest Ordered Subsequence(简单的DP)
- POJ 3061 Subsequence 【尺取法】
- POJ 3061 · Subsequence【子序列】【二分】
- poj 3061 Subsequence(尺取)
- POJ 3061 Subsequence [尺取法] 《挑战程序设计竞赛》3.2
- poj 3061 Subsequence 尺取
- POJ 3061 Subsequence 尺取
- [POJ 3061] Subsequence