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++];

16级第一周寒假作业F题的更多相关文章

  1. 福建工程学院16级第一周寒假作业E题----第七集,奇思妙想

    第七集,奇思妙想                                                                                            ...

  2. 16级第二周寒假作业H题

    快速幂(三) TimeLimit:2000MS  MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 计算( AB ...

  3. 16级第二周寒假作业E题

    Home_W的位运算4 TimeLimit:2000MS  MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 给 ...

  4. FJUT16级第一周寒假作业题解I题

    涨姿势题3 TimeLimit:1000ms  MemoryLimit:128000KB 64-bit integer IO format:%lld Problem Description 涨姿势题就 ...

  5. FJUT16级第一周寒假作业题解G题

    题目链接:http://210.34.193.66:8080/vj/Contest.jsp?cid=160#P6 涨姿势题1 TimeLimit:1000MS  MemoryLimit:128000K ...

  6. FJUT16级第一周寒假作业题解J题

    题目链接:http://210.34.193.66:8080/vj/Contest.jsp?cid=160#P9 涨姿势之区间刷新 TimeLimit:2000MS  MemoryLimit:128M ...

  7. FJUT16级第一周寒假作业题解D题

    题目链接:http://210.34.193.66:8080/vj/Contest.jsp?cid=160#P3 第八集,体能训练 TimeLimit:1000MS  MemoryLimit:128M ...

  8. 江西财经大学第一届程序设计竞赛 F题 解方程

    链接:https://www.nowcoder.com/acm/contest/115/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  9. Python 第一周编程作业

    一.  编程题 1.  结合turtle库使用手册,读懂下列代码,并在jupyter编译器中运行观察结果: 依次分析下代码: 第一行 通过保留字import引用了Python中用于绘制图形的turtl ...

随机推荐

  1. spark开发

    1. 主要参考资料http://spark.incubator.apache.org/docs/latest/scala-programming-guide.htmlhttp://www.eecs.b ...

  2. a/b + c/d

    a/b + c/d Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  3. shell之路【第二篇】运算与文件调用

    Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,默认都是字符串操作,但是可以通过其他命令来实现 算数运算 expr. ...

  4. 确定当前Python环境中的site-packages目录位置

    引入“搜索路径”这个概念是因为在使用import语句时,当解释器遇到import语句,如果模块在当前的搜索路径就会被导入. 搜索路径是一个解释器会先进行搜索的所有目录的列表. 那么python如何添加 ...

  5. Perl 之 use(), require(), do(), %INC and @INC

    来源: http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html 转自:http://perl.apache.org/docs/gene ...

  6. vim 设置默认显示行号

    vim 要默认打开的时候显示行号,这样就可以不用每次编辑的时候在手动在命令模式下:set number 来显示行号. 具体做法是: 创建一个 .vimrc的配置文件. 在启动vim时,当前用户根目录下 ...

  7. calc()使用笔记

    calc()可以给元素做运算, calc(expression) eg: .a { width: -webkit-calc(100% - 2px); width: -moz-calc(100% - 2 ...

  8. 最佳死链接href= "#" VS href= "javascript:void(0);"

    href= "#" 熟悉hash跳转的同学都知道,只是一个hash跳转,跳转到页面的顶部,可以发现地址栏多了一个#,点击返回会返回本页面. href= "javascri ...

  9. Jquery 实现原理之 Ajax

    一:Jquery Ajax底层接口有:$.ajaxPrefilters.$.ajaxTransport.$.ajaxSettings.$ajaxSetup.$ajaxSettings; 其中$.aja ...

  10. 最短路<dijk>

    题意: 有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路.按格式输出. power oj 2443 题解: 标准dijk算法. #include<c ...