poj-【3061 Subsequence】
2016-11-29 16:18
357 查看
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
int num[100000+11];
int get_min(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,s;
scanf("%d%d",&n,&s);
for(int i=0;i<n;++i)
scanf("%d",&num[i]);
int ans=n+1,i=0,j=0,sum=0;
while(1)
{
while(j<n&&sum<=s)
sum+=num[j++];
if(sum<s)
break;
ans=get_min(j-i,ans);
sum-=num[i++];
}
if(ans>n)
ans=0;
printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12980 | Accepted: 5489 |
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
23#include<cstdio>
int num[100000+11];
int get_min(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,s;
scanf("%d%d",&n,&s);
for(int i=0;i<n;++i)
scanf("%d",&num[i]);
int ans=n+1,i=0,j=0,sum=0;
while(1)
{
while(j<n&&sum<=s)
sum+=num[j++];
if(sum<s)
break;
ans=get_min(j-i,ans);
sum-=num[i++];
}
if(ans>n)
ans=0;
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- poj 3061 Subsequence
- Poj 3061 Subsequence(二分+前缀和)
- POJ 3061:Subsequence 查找连续的几个数,使得这几个数的和大于给定的S
- 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
- poj 3061 Subsequence
- poj-3061-Subsequence(二分思想)
- poj--3061--Subsequence(贪心)
- poj 3061 Subsequence【简单尺取】
- [尺取法] poj 3061 Subsequence