A
来源:互联网 发布:知乎怎么用 编辑:程序博客网 时间:2024/05/18 01:23
、点击打开链接
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.
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.
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5
23
题解:满足条件的最短连续子序列元素个数;
今天说是练习二分法,可是我自己用其他的方法也能解决
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int maxn=100009;int a[maxn];int n,s;int main(){int t;while(scanf("%d",&t)!=EOF){for(int i=0;i<t;i++){scanf("%d%d",&n,&s);for(int j=1;j<=n;j++){scanf("%d",&a[j]);}int w=1,e=1,sum=0,ans=n+1;for(;;){while(e<=n&&sum<s){sum+=a[e++];}if(sum<s) break;ans=min(ans,e-w);sum-=a[w++];}if(ans==n+1) printf("0\n");else printf("%d\n",ans);}}return 0;}
阅读全文
0 0
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- springframework Request method 'GET' not supported
- hibernate与spring整合:配置事务之后并不会滚的问题
- ubutu搭建pptp
- KMP字符串模式匹配算法实现
- 【面试题】判断一棵二叉树是否是完全二叉树
- A
- jvm--gc垃圾收集(1)
- Linux的各文件目录作用
- VMware vSphere Web Services SDK编程指南(二)-Java 开发设置
- [SAM] Spoj1811 LCS
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
- CSS--基础知识
- Hibernate注解详解
- Maven环境搭建