1. #include <iostream> //nlogn复杂度的写法
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. const int INF = 0x3f3f3f3f;
  7. const int moder = 1e9 + ;
  8. const int maxn = ;
  9.  
  10. int a[maxn];
  11.  
  12. int main()
  13. {
  14. int t;
  15. cin >> t;
  16. while(t--)
  17. {
  18. int n,s;
  19. scanf("%d%d",&n,&s);
  20. for(int i=;i <= n;i++)
  21. scanf("%d",&a[i]);
  22.  
  23. for(int i=;i <= n;i++)
  24. {
  25. a[i] = a[i] + a[i-];
  26. }
  27. if(a[n] < s) printf("0\n");
  28. else
  29. {
  30. int res = n;
  31. for(int i=;a[n]-a[i] >= s;i++)
  32. {
  33. int t = lower_bound(a+,a+n+,s+a[i]) - a; // - a 非 - a - 1
  34. res = min(res,t-i);
  35. }
  36. printf("%d\n",res);
  37. }
  38. }
  39. return ;
  40. }
  41. /*2
  42. 10 15
  43. 5 1 3 5 10 7 4 9 2 8
  44. 5 11
  45. 1 2 3 4 5 */

O(n)复杂度的写法

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. const int INF = 0x3f3f3f3f;
  7. const int moder = 1e9 + ;
  8. const int maxn = ;
  9.  
  10. int a[maxn];
  11.  
  12. int main()
  13. {
  14. int t;
  15. cin >> t;
  16. while(t--)
  17. {
  18. int n,s;
  19. scanf("%d%d",&n,&s);
  20. for(int i=;i <= n;i++)
  21. scanf("%d",&a[i]);
  22. int i=,j=;
  23. int sum=;
  24. int res = n+;
  25. for(;;)
  26. {
  27. while(i <= n && sum < s)
  28. {
  29. sum += a[i];
  30. i++;
  31. }
  32. if(sum < s) break;
  33. res = min(res,i-j);
  34. sum = sum - a[j];
  35. j++;
  36. }
  37. if(res > n) res = ;
  38. printf("%d\n",res);
  39. }
  40. return ;
  41. }
  42. /*2
  43. 10 15
  44. 5 1 3 5 10 7 4 9 2 8
  45. 5 11
  46. 1 2 3 4 5 */

——

尺取法——POJ3061的更多相关文章

  1. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

  2. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  3. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

  4. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  5. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  6. poj3061尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  7. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

  8. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  9. POJ 3061 Subsequence 尺取法

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

随机推荐

  1. Meaning of “const” last in a C++ method declaration?

    函数尾部的const是什么意思? 1 Answer by Jnick Bernnet A "const function", denoted with the keyword co ...

  2. 当Web访问性能出现问题,如何深探?

    对运维或开发工程师来说,遇到访问性能问题时,最先需要定位的是问题出现在哪个环节,是网络的问题,服务端的问题,还是客户端的问题? 往往技术人员喜欢把精力放在保障后端服务的可用性方面,而对前端界面是否能正 ...

  3. nodejs学习笔记Node.js 调试命令

    3.4  调试        47  下面是一个简单的例子: $ node debug debug.js < debugger listening on port 5858 connecting ...

  4. linux彻底删除nginx

    卸载 删除 nginx 1.删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1 2.自动移除全部不使用的软件包 sudo apt-get ...

  5. conda

    Conda是什么? Conda 是Anaconda下用于包管理和环境管理的命令行工具, Conda下一切都是包,包括Python和conda自己 Conda ≍ pip(包管理) + vitualen ...

  6. mysql 约束条件 auto_increment 自动增长目录

    mysql 约束条件 auto_increment 自动增长 mysql 约束条件 auto_increment 自动增长起始值 布长 起始偏移量 mysql 约束条件 auto_increment ...

  7. github使用方法

    1:fork别人的项目后,更新别人最新的提交 https://blog.csdn.net/qq1332479771/article/details/56087333 2:在idea中push工程    ...

  8. spring requestmapping 拦截多个请求

    @RequestMapping(value = {"/picture_content/{id}","/attachment_content/{id}",&quo ...

  9. 记一次mysql启动不了的问题

    在linux上用的是xampp,mysql启动没有报任何错误,但就是查找不到进程,于是找mysql错误日志,日志在哪?在lampp/var/mysql 以.err结尾的文件里.里面内容如下; /opt ...

  10. python学习之路-day10

    一.什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程. 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程. 车间负责把资源 ...