Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14698   Accepted: 6205

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.

Sample Input

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

Sample Output

  1. 2
  2. 3

Source

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<deque>
  9. #include<iomanip>
  10. #include<vector>
  11. #include<cmath>
  12. #include<map>
  13. #include<stack>
  14. #include<set>
  15. #include<fstream>
  16. #include<memory>
  17. #include<list>
  18. #include<string>
  19. using namespace std;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. #define MAXN 100004
  23. #define L 31
  24. #define INF 1000000009
  25. #define eps 0.00000001
  26. /*
  27. 尺取
  28. */
  29. int a[MAXN], n, s;
  30. int main()
  31. {
  32. int t;
  33. scanf("%d", &t);
  34. while (t--)
  35. {
  36. scanf("%d%d", &n, &s);
  37. for (int i = ; i < n; i++)
  38. scanf("%d", &a[i]);
  39. int l = ,sum = ,ans = INF;
  40. for (int r = ; r < n; r++)
  41. {
  42. sum += a[r];
  43. while (sum >= s)
  44. {
  45. sum -= a[l];
  46. ans = min(r - l + , ans);
  47. l++;
  48. }
  49. }
  50. if (ans != INF)
  51. printf("%d\n", ans);
  52. else
  53. printf("0\n");
  54. }
  55. }

POJ 3061 Subsequence 尺取的更多相关文章

  1. POJ 3061 Subsequence 尺取法

    转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...

  2. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  3. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  4. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  5. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

  6. poj 3061 Subsequence

    题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...

  7. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  8. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  9. POJ 3061 Subsequence 二分或者尺取法

    http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...

随机推荐

  1. E20171016-mk

    chaos   n. 混乱,紊乱; (天地未出现的) 浑沌世界; 〈古〉无底深渊; 一团糟;

  2. In 7-bit

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3713 题意:给定一个字符串,首先输出这个字符串的长度(以两位的十六进制的形 ...

  3. vue-video-player视频播放插件

    安装依赖 npm install vue-video-player -S 引入配置 //main.js import 'video.js/dist/video-js.css' import 'vue- ...

  4. Oh,mygoddess

    很早的时候就看了这一道题目 , 当时不会做 , 现在 边听歌边写无压力 ........ 题意 : 光辉骑士 一直都在 迷宫的右上角 , 第一行给你迷宫的规格 , 下面是迷宫 "O" ...

  5. Sum It Up -- 深搜 ---较难

    每一行都是一组测试案例   第一个数字 表示总和 第二个数字表示 一共有几个可用数据  现在 按照从小到大的顺序   输出  那些数字中若干数字之和为总和的  信息 /. 很好很明显的  遍历痕迹 , ...

  6. 普通平衡树代码。。。Treap

    应一些人之邀...发一篇代码 #include <iostream> #include <cstdio> #include <cstdlib> #include & ...

  7. [转]mysql视图学习总结

    转自:http://www.cnblogs.com/wangtao_20/archive/2011/02/24/1964276.html 一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个 ...

  8. DML和DQL语句

    DML操作: a.插入单行数据:      INSERT INTO 表名(列名,列名,...) VALUE(列值,列值,...); 表的字段名是可选的,如果省略,则一次插入所有字段 多个列表和多个值之 ...

  9. CSS实现两栏布局

    写在前面 两栏布局是指页面布局由主栏和边栏组成,是许多网页的布局方式,一般使用CSS去实现两栏布局. 实现两栏布局的方式有多种,这里采用四种比较常见的实现方式.主要是流体布局(liquid layou ...

  10. Ajax——模板引擎

    模板介绍 1.必要性:ajax请求从服务器接收到大量数据,此时再用普通的字符串拼接是很耗费时间的,这时候模板就有其必要性 2.便利性:插件套用,现在有很多主流的模板插件:BaiduTemplate(百 ...