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. Q promise API简单翻译

    详细API:https://github.com/kriskowal/q/wiki/API-Reference Q提供了promise的一种实现方式,现在在node中用的已经比较多了.因为没有中文的a ...

  2. CentOS,Ubuntu,Gentoo,Freebsd,RedHat,Debian的区别及选择

    Linux最早由Linus Benedict Torvalds在1991年开始编写.在这之前,Richard Stallman创建了Free Software Foundation(FSF)组织以及G ...

  3. how to use tar?

    In UNIX, tar is the most useful tool to compress files (just like zip in Windows.) To compress, inpu ...

  4. kubernetes 条件需求

    1. 你必须拥有一台安装有Docker的机器. 2. 你的内核必须支持 memory and swap accounting .确认你的linux内核开启了如下配置: CONFIG_RESOURCE_ ...

  5. .htaccess重写URL讲解

    使用ThinkPHP和Laravel等框架的都知道,所以的请求都需要经过index.php文件入口,无论你的URI是什么. 当然除了访问的是静态文件或者访问路径的文件真实存在,例如你访问xxx.com ...

  6. 状压dp找寻环的个数 Codeforces Beta Round #11 D

    http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...

  7. perl中调用cgi

    来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698856.html 参考:http://www.willmaster.com/librar ...

  8. wpf CollectionViewSource的运用

    实体类: 员工类: public class Department : ObservableCollection<Employee> { public string DepName { g ...

  9. java方法:flush()

    flush本意是冲刷,这个方法大概取自它引申义冲马桶的意思,马桶有个池子,你往里面扔东西,会暂时保存在池子里,只有你放水冲下去,东西才会进入下水道. 同理很多流都有一个这样的池子,专业术语叫缓冲区,当 ...

  10. Hibernate 系列教程4-单向多对一

    项目图片 hibernate.cfg.xml <mapping resource="com/jege/hibernate/one/way/manytoone/User.hbm.xml& ...