Subsequence

TimeLimit:1000MS  MemoryLimit:65536K
64-bit integer IO format:%lld
Problem 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.
SampleInput

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

SampleOutput
2
3

思路:题目的意思就是给定长度为n的整数数列以及整数S,求出总和不小于S的连续子序列的长度的最小值,如果解不存在,输出0.

个人对尺取的看法:当a1,a2,a3满足条件>=S的时候,我们得到区间长度为3,去掉第一个元素a1,我们在判断a2,a3是否符合条件,如果满足,区间长度更新为2,如果不满足,就将尾部向后扩展,判断a2,a3,a4是否符合,如果符合区间长度更新为3,如果不符合就再向后扩展,一直重复这个操作。

所以我们就用尺取法,先找到总和不小于S的区间,然后用ans去记忆符合条件的区间长度 ,然后再找符合符合总和不小于S的区间,然后用这个新区间的长度跟ans比较,比ans小就赋值,周而复始,一直到区间走到数组的最后一个为止,就可以了;

晒上核心代码

(i<N&&sum<S;i++)
sum+=a[i];
if(sum<S)
break;
ans=ans<i-s?ans:i-s;
sum-=a[s++];

最新文章

  1. 【6年开源路】海王星给你好看!FineUI v4.0正式版暨《FineUI3to4一键升级工具》发布!
  2. RestEasy简介
  3. [AHOI2006]文本编辑器 Splay tree区间操作
  4. Codeforces 295C Greg and Friends
  5. 【HDOJ】3061 Battle
  6. Spark性能调优之代码方面的优化
  7. spring集成mybatis实现mysql读写分离
  8. python_code list_2
  9. [SCOI2005]扫雷
  10. Amazon
  11. ML: 聚类算法R包 - 模型聚类
  12. 使用curator框架简单操作zookeeper 学习笔记
  13. 软件工程实践-git的使用
  14. 通用的Bitmap压缩算法,进一步节约内存(推荐)
  15. 滴滴大数据算法大赛Di-Tech2016参赛总结
  16. 统计无向图中三角形的个数,复杂度m*sqrt(m).
  17. 数据库触发器,禁止DDL操作
  18. __autoload自动加载类
  19. POJ3662电缆
  20. appium===常用方法介绍,元素定位

热门文章

  1. javascript中的throttle和debounce
  2. hdu_1513_Palindrome(LCS+滚动数组)
  3. IntelliJ IDEA 7.0 正式版注册机代码
  4. Linq第二讲
  5. docker log 文件 清理
  6. 关于ckeditor过滤掉html样式标签之我见
  7. flex日期合并与拆分
  8. Django urls常用匹配语法
  9. SQL查询表,表的所有字段名,SQL查询表,表的所有字段名
  10. 关于RuntimException