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. 《JS权威指南学习总结--第三章类型、值和变量》

    第三章 类型.值和变量 内容要点 一.数据类型 1.在编程语言中,能够表示并操作的值的类型称做数据类型 2.JS的数据类型分为两类: 原始类型:数字.字符串和布尔值 对象类型 3.JS中有两个特殊的原 ...

  2. Centos7下安装pip

    Linux 通过 pip 安装使用 Shadowsocks - CentOS 7 (06) Pip是安装Python包的工具,提供了安装.列举已安装包.升级以及卸载包的功能.Pip 是对easy_in ...

  3. 指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效

    以下内容来自互联网 (1)web: 需要在客户端配置文件的中增加connectionString节点,此节点描述了EntityClient的连接信息. 例如: 在web.config的中增加conne ...

  4. 天津工业大学CST专业培养方案

    第二学期 网页设计基础 计算机前沿技术 大学生心理健康与人生发展 高级语言程序设计II 思想道德修养与法律基础 职业生涯规划 大学英语二级(A) 大学物理(理三1) 线性代数 高等数学(理一2) 体育 ...

  5. js截取文件名

    str = 'C:\fakepath\ll.doc'; str.substring(str.lastIndexOf("\\")+1,str.lastIndexOf(".& ...

  6. Lvs+Keepalived+Bind+web构建高可用负载均衡系统

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://hatech.blog.51cto.com/8360868/1417899 --- ...

  7. wl18xx编译的时候出现WARNING: "simple_open" WARNING: "wl12xx_get_platform_data"

     ................................................................................................... ...

  8. Oracle自增长序列

    create table user_info ( id number(6) primary key, username varchar2(30) not null, password varchar2 ...

  9. OpenGL中glFrustum()和gluPerspective()的相互转换

    OpenGL中在窗口的大小发生变化的时候会触发resize()函数,这里会传入一个新的宽和高,在resize()函数中我们会设置投影矩阵,在可以使用OpenGL基础函数glFrustum()函数和gl ...

  10. 关于jQuery的条件判断问题

    遇到的第一个问题,点击第一行用toggle实现背景的变换,需要在toggle函数里面判断,当下面显示的时候是一个状态,下面不显示的时候是另一个状态,我用了 if($(".check-box& ...