Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22040   Accepted: 9404

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

 
题意:给定长度为n的数列整数,及整数S。求出总和不小于S的连续子序列的长度的最小值
思路:尺取法 用queue进行维护就可以哒~
 
acode
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<string>
  7. #include<vector>
  8. #include<set>
  9. #include<stack>
  10. #include<queue>
  11. #include<map>
  12. #include<cmath>
  13. #include<algorithm>
  14. using namespace std;
  15. #define inf 0x3f3f3f3f
  16. #define ll long long
  17. #define MAX_N 1000005
  18. #define gcd(a,b) __gcd(a,b)
  19. #define mem(a,x) memset(a,x,sizeof(a))
  20. #define mid(a,b) a+b/2
  21. #define stol(a) atoi(a.c_str())//string to long
  22. int temp[MAX_N];
  23. int main(){
  24. //std::ios::sync_with_stdio(false);
  25. //std::cin.tie(0);
  26. // #ifndef ONLINE_JUDGE
  27. // freopen("D:\\in.txt","r",stdin);
  28. // freopen("D:\\out.txt","w",stdout);
  29. // #else
  30. // #endif
  31. int T;
  32. scanf("%d",&T);
  33. int N,S;
  34. while(T--){
  35. scanf("%d%d",&N,&S);
  36. for(int i = ; i < N; i++)
  37. scanf("%d",&temp[i]);
  38. int sum = ;
  39. queue<int> que;
  40. int res = inf;
  41. for(int i = ; i < N; i++){
  42. que.push(temp[i]);
  43. sum += temp[i];
  44. while(sum >= S){
  45. res = min(res,(int)que.size());
  46. sum -= que.front();
  47. que.pop();
  48. }
  49. }
  50. if(res!=inf)
  51. printf("%d\n",res);
  52. else
  53. printf("0\n");
  54. }
  55. return ;
  56. }

Subsequence POJ - 3061的更多相关文章

  1. Subsequence poj 3061 二分(nlog n)或尺取法(n)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 Descr ...

  2. Greedy:Subsequence(POJ 3061)

      和最短序列 题目大意:找出一个序列中比至少和S相等的最短子序列(连续的) 本来这道题可以二分法来做复杂度O(NlogN),也可以用一个类似于游标卡尺的方法O(N)来做 先来讲游标卡尺法: 因为子序 ...

  3. poj 3061 Subsequence

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

  4. POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)

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

  5. POJ 3061 Subsequence(Two Pointers)

    [题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子 ...

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

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

  7. poj 3061 Subsequence 二分 前缀和 双指针

    地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...

  8. POJ 3061 Subsequence(尺取法)

    题目链接: 传送门 Subsequence Time Limit: 1000MS     Memory Limit: 65536K 题目描述 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子 ...

  9. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

随机推荐

  1. 算法笔记4.3递归 问题 A: 吃糖果

    问题 A: 吃糖果 题目描述 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0). 妈妈告诉名名每天可以吃一块或者两块巧克力. 假设名 ...

  2. python3下scrapy爬虫(第十四卷:scrapy+scrapy_redis+scrapyd打造分布式爬虫之执行)

    现在我们现在一个分机上引入一个SCRAPY的爬虫项目,要求数据存储在MONGODB中 现在我们需要在SETTING.PY设置我们的爬虫文件 再添加PIPELINE 注释掉的原因是爬虫执行完后,和本地存 ...

  3. 通俗易懂JSONP讲解

    原文地址:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html JSON的格式或者叫规则: JSON能够 ...

  4. 用Microsoft Chart Controls(MSChart)实现曲线图,并支持拖动放大到秒

    Microsoft Chart Controls(简称MSChart)控件,给图形统计和报表图形显示提供了很好的解决办法,同时支持Web和WinForm两种方式. MSChart 在.NET 4.0自 ...

  5. Sampling Error|Sampling mean|population mean

    7.1 Sampling Error; the Need for Sampling Distributions 样本均值的三种表达: Sampling distribution of the samp ...

  6. 提前窥测奥斯卡颁奖信封中的谜底  ——Rothschild预测2014奥斯卡花落谁家

     --Rothschild预测2014奥斯卡花落谁家" title="提前窥测奥斯卡颁奖信封中的谜底  --Rothschild预测2014奥斯卡花落谁家"> 编者 ...

  7. deeplearning.ai 构建机器学习项目 Week 2 机器学习策略 II

    1. 误差分析(Error analysis) 误差分析的目的是找到不同误差源的比重,从而指引我们接下来往哪个方向努力改进.NG建议手工统计随机100个错误的误差源,比如对于猫分类器,错误的照片可能是 ...

  8. Serializable中的serialVersionUID是必须的吗

    不写serialVersionUID就没有吗 即使不写, jdk反序列化时也会自动检查这个id, 反编译.class文件你也看不到这个值 rpc反序列化 如果使用jdk的方式, 这个必须配置 如果使用 ...

  9. SpringBoot连接Oracle报错,找不到驱动类,application.properties文件中驱动类路径为红色

    pom.xml文件: <!-- oracle odbc --> <dependency> <groupId>com.oracle</groupId> & ...

  10. reduced penetrance|COPE-PCG

    生物医学大数据 Case study 由Human genome project提出之后,提出的精准医学.它的初衷是将数据standard后easy应用,我国重要重在疾病预警和疗效评价. 在疾病预警上 ...