---恢复内容开始---

Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10487   Accepted: 4337

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个一连串的数字,求不小于m的最短的连续的数字长度;
分析:暴力的话复杂度n^2,肯定会超时,有两种方法,都要掌握,第一种是lower_bound函数的使用,lower_bound的复杂度是log(n)。所以该算法的复杂度是nlog(n),相比于暴力,算法的优化仅仅体现在使用了lower_bound将n变为了log(n).
  1. #include<cstdio>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <set>
  11. using namespace std;
  12. #define MM(a) memset(a,0,sizeof(a))
  13. typedef long long LL;
  14. typedef unsigned long long ULL;
  15. const int mod = 1000000007;
  16. const double eps = 1e-10;
  17. const int inf = 0x3f3f3f3f;
  18. long long mid,l,r,n,m;
  19. int sum[100005];
  20. int main()
  21. {
  22. int n,cas,s,k,ans,res;
  23. cin>>cas;
  24. while(cas--)
  25. {
  26. scanf("%d %d",&n,&s);
  27. sum[0]=0;ans=100005;
  28. for(int i=1;i<=n;i++)
  29. {
  30. scanf("%d",&sum[i]);
  31. sum[i]+=sum[i-1];   //将数组求和是常用的技巧
  32. }
  33. if(sum[n]<s)
  34. {
  35. cout<<"0"<<endl;
  36. continue;
  37. }
  38. for(k=0;sum[n]-sum[k]>=s;k++)  //枚举起点
  39. {
  40. res=lower_bound(sum+k,sum+n+1,sum[k]+s)-(sum+k);  //第一个大于等于该值的位                                          //置
  41. if(res<ans)
  42. ans=res;
  43. }
  44. printf("%d\n",ans);
  45. }
  46. return 0;
  47. }

  下面重点介绍尺取法:

  尺取法的核心思想:假设当前a[s]+s[s+1]+...a[t]是最初>=sum的,那么当s变为s+1时,即a[s+1]+s[s+2]+...a[t+n]要想仍然成为最初>=sum的,则t+n>=t(a[s]可能为0),依据这一思想,可以设置两个”指针“,一个指向p一连续序列的开头位置,另一头q指向结尾位置,该子序列之和>=sum,则当p向右推移时,则q也应向右推移直到两指针之间的序列之和再次最初>=sum,复杂度是O(n),其实觉得尺取法最显著的优势就在于能够保存中间一部分子序列的计算结果,这也是其复杂度更低的原因。

  1. #include<cstdio>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <set>
  11. using namespace std;
  12. #define MM(a) memset(a,0,sizeof(a))
  13. typedef long long LL;
  14. typedef unsigned long long ULL;
  15. const int mod = 1000000007;
  16. const double eps = 1e-10;
  17. const int inf = 0x3f3f3f3f;
  18. long long mid,l,r,n,m;
  19. int a[100005];
  20. int main()
  21. {
  22. int n,cas,s;
  23. cin>>cas;
  24. while(cas--)
  25. {
  26. scanf("%d %d",&n,&s);
  27. a[0]=0;a[n+1]=0;
  28. for(int i=1;i<=n;i++)
  29. scanf("%d",&a[i]);
  30. int p=0,q=0,v=0,ans=n+1;
  31. while(v<s&&q<=n)
  32. {
  33. q++;
  34. v+=a[q];
  35. }
  36. if(q==n+1)
  37. {
  38. cout<<"0"<<endl;
  39. continue;
  40. }
  41. for(;;)
  42. {
  43. while(v<s&&q<=n)
  44. {
  45. q++;
  46. v+=a[q];
  47. }
  48. if(q==n+1)
  49. break;
  50. if(ans>q-p)
  51. ans=q-p;
  52. p++;
  53. v-=a[p];
  54. }
  55. printf("%d\n",ans);
  56. }
  57. return 0;

  

POJ 3061  Subsequence   尺取法   挑战146页的更多相关文章

  1. POJ 3061 Subsequence(尺取法)

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

  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 尺取法

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

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

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

  5. poj 3061 题解(尺取法|二分

    题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...

  6. POJ 3061 Subsequence 尺取

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

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

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

  8. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

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

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

  10. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

随机推荐

  1. [转帖]rpm包和deb分别是什么?

    https://www.cnblogs.com/hanfanfan/p/9133789.html 需要不停的学习才可以. 一.RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了.这 ...

  2. 【Python】【demo实验3】【显示素数,显示两个数范围内的所有素数】

    打印两个整数之间的所有素数: (使用平方根来判断  是否应停止验证该数值是否为素数) for i in range(956253526252,9956253526252): k = 1 if i == ...

  3. Python_4day

    函数 函数可以用来定义可重复代码,组织和简化 一般来说一个函数在实际开发中为一个小功能 一个类为一个大功能 同样函数的长度不要超过一屏   Python中的所有函数实际上都是有返回值(return N ...

  4. 高效编程之 cProfile 性能分析

    写代码经常会听说一些名词,比如 性能分析.代码调优. cProfile 是 python 代码调优的一种工具,它能够统计在整个代码执行过程中,每个函数调用的次数和消耗的时间. 这个工具虽然很常用,但是 ...

  5. CSP/NOIP 2019 游记

    Day0 打牌 Day1 \(T1\) 没开\(ull\), 不知道有几分 \(T2\) \(N^2\)暴力+链, 没搞出树上做法, \(70\)分 \(T3\) 标准\(10\)分( 感觉今年省一稳 ...

  6. Bicolored RBS CodeForces - 1167D (括号)

    建树, 然后高度最大值的最小值显然为$\lceil \frac{dep}{2}\rceil$, 将$>\frac{dep}{2}$的全部分出去即可. #include <sstream&g ...

  7. 记一次完整的java项目压力测试

    总结:通过这次压力测试,增加了对程序的理解:假定正常情况下方法执行时间为2秒,吞吐量为100/s,则并发为200/s:假设用户可接受范围为10s,那么并发量可以继续增加到1000/s,到这个时候一切还 ...

  8. Mysql学习(一)之简单介绍

    数据库简介 数据库分类 关系型数据库:MySQL.Oracle.SQLServer.Access.db2.fox pro 文件型数据库:sqlite.mongodb 空间型数据库: 数据库分为两端 数 ...

  9. 强大的开源企业级数据库监控利器Lepus

    Lepus监控简单介绍 官方网站:http://www.lepus.cc 开源企业级数据库监控系统 简洁.直观.强大的开源数据库监控系统,MySQL/Oracle/MongoDB/Redis一站式性能 ...

  10. mac终端解决很多系统自带命令找不到问题

    node安装提示npm command not found 1.打开终端 2.输入命令如下: touch~/.bash_profile  (创建.bash_profile文件,-表示在-目录下,.表示 ...