poj3061
2016-03-09 22:56
295 查看
Subsequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10916 Accepted: 4516
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
#include<stdio.h>
#define INF 0x3f3f3f
int min(int x,int y)
{
return x>y? y:x;
}
int a[1000010];
int solve(int n,int s)
{
int i=0,j=0,k,rec=INF,sum=0;
while(1)
{
for(;i<n&&sum<s;i++)
{
sum+=a[i];
}
if(sum<s) break;
rec=min(rec,i-j);
sum-=a[j++];
}
if(rec==INF) printf("0\n");
else printf("%d\n",rec);
}
int main()
{
int t,i,j,k,s,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&s);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
solve(n,s);
}
return 0;
}
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10916 Accepted: 4516
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
#include<stdio.h>
#define INF 0x3f3f3f
int min(int x,int y)
{
return x>y? y:x;
}
int a[1000010];
int solve(int n,int s)
{
int i=0,j=0,k,rec=INF,sum=0;
while(1)
{
for(;i<n&&sum<s;i++)
{
sum+=a[i];
}
if(sum<s) break;
rec=min(rec,i-j);
sum-=a[j++];
}
if(rec==INF) printf("0\n");
else printf("%d\n",rec);
}
int main()
{
int t,i,j,k,s,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&s);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
solve(n,s);
}
return 0;
}
相关文章推荐
- Android签名验证简介
- leetcode:Delete Node in a Linked List 【Java】
- 001.一个.java源文件中是否可以包含多个类(不是内部类)?有什么限制?
- 用命令行编译运行java文件的乱码问题
- RAR暴破
- Fire Game
- PHP——上传头像(1)
- UVA 12034 Race 赛马名次
- cvReleaseImage出错问题
- Ubuntu Sun JDK 安装
- unable to start the virtual devices
- 初识Java设计模式之单例设计模式
- Linux内核设计第三周——构造一个简单的Linux系统
- 关于hql语句的一些问题
- 编程小练习
- 运用Ajax和JSON对象实现JSP和Servlet的数据传递
- 讲故事,学(Java)设计模式—桥接模式
- Ubuntu安装Sun JDK及如何设置默认java JDK
- ADS
- jdk安装与环境变量的配置