Subsequence

时间限制: 1000ms 内存限制: 65536KB

通过次数: 1总提交次数: 1

问题描述

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.

输入描述

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.

输出描述

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

样例输入

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

样例输出

2
3

来源

Southeastern Europe 2006

问题分析:(略)

这个问题和《POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】》是同一个问题,代码直接用就AC了。

程序说明:参见参考链接。

参考链接:POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】

题记:程序做多了,不定哪天遇见似曾相识的。

AC的C++程序如下:

/* POJ3061 ZOJ3123 Subsequence */#include <iostream>
#include <algorithm>using namespace std;const int N = 100000;
int prefixsum[N+1];int main()
{int t, n, s, val, ans;cin >> t;while(t--) {cin >> n >> s;// 输入数据,计算前缀和prefixsum[0] = 0;for(int i=1; i<=n; i++) {cin >> val;prefixsum[i] = prefixsum[i - 1] + val;}if(prefixsum[n] < s)ans = 0;else {ans = n;for(int i=0; prefixsum[i] + s < prefixsum[n]; i++) {int pos = lower_bound(prefixsum + i, prefixsum + n, prefixsum[i] + s) - prefixsum;ans = min(ans, pos - i);}}cout << ans << endl;}return 0;
}

NUC1742 Subsequence【前缀和+二分搜索+尺取法】相关推荐

  1. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  2. hud 1003 Max Snm(最小前缀和、尺取法)

    题意:在o(n)时间内,求最大连续的子序列的和最大,及其起点和终点. 数据:输入以及输出 方法一:一边读取数据一边维护最小前缀和S[i],然后不断更新ans,ans=max(ans,s[i]-s[k] ...

  3. poj3061尺取法/前缀和 二分(java)

    今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...

  4. Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)

    题意: 给定一个数组和一个值t,求一个子区间使得其和的绝对值与t的差值最小,如果存在多个,任意解都可行. 题目: Signals of most probably extra-terrestrial ...

  5. (二分搜索法尺取法)subsequence

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

  6. (尺取法模板题) QLU_ACM 2021 专题训练(一) D - Subsequence 题解

    D - Subsequence POJ - 3061 <----------- 原题在这 题目大意 给定数组a,求其符合∑a[i] > S 的最短子数列. 尺取法解析 蒟蒻这道题WA了8次 ...

  7. 尺取法 POJ 3601 Subsequence

    题目传送门 1 /* 2 题意:求连续子序列的和不小于s的长度的最小值 3 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 4 1. 记录前i项的总和,求[i, p)长度的最小值,用二分 ...

  8. POJ-2566,HDU-1058,POJ-3320,POJ-3061(尺取法)

    题目链接: Bound Found 思路 用前缀和先统计每个数到第一个数的总和.因为尺取法需要数组有序,所以对前缀和得到的数组排序.设立两个类似指针了l,r,使得l,r不断前进,并记录这之间产生的最小 ...

  9. 解题报告 (十三) 尺取法

    文章目录 尺取法 解题报告 PKU 2100 Graveyard Design PKU 3061 Subsequence PKU 2739 Sum of Consecutive Prime Numbe ...

最新文章

  1. 漫画:最长公共子序列
  2. About darwin OS
  3. 2018年各大互联网前端面试题三(阿里)
  4. html css图标怎么跟文字并排,CSS高级技巧:精灵图、字体图标、CSS三角做法、CSS用户界面样式、vertical-align属性应用、溢出文字省略号、常见布局技巧...
  5. SAP Spartacus 的 git flow 和发布流程
  6. Directed Roads CodeForces - 711D (基环外向树 )
  7. Qt工作笔记-在QTreeView上实现模型数据的拖拽
  8. 2015 百度一面 总结记录
  9. 联想ghost重装系统_联想一键ghost重装系统步骤
  10. 数据:BTC全网算力为146.06 EH/s,新增地址数51.19万
  11. 空间波(space wave)
  12. 【会议记录】软件工程课程设计第一次会议
  13. 大数据平台基础架构hadoop安全分析
  14. 电子元件-双向触发二极管与可控硅
  15. 安装studio3t
  16. 爬取斗鱼主播名字和热度
  17. Olly Advanced 1.27
  18. xv6-lab2-syscall
  19. mysql存储过程中使用select count(*) into 变量名 from +表+ where条件的用法
  20. ettercap局域网内DNS欺骗

热门文章

  1. MFC 教程【7_MFC的DLL 】
  2. onepill服务端
  3. 详解:MapReduce 思想解析
  4. python计算信息增益_利用Python提取ABAQUS的计算结果(ODB)信息、体积、应变等变化(一)...
  5. scala中的filter函数
  6. scala的map映射问题
  7. ediplus 复制编辑一列_Excel中如何使用公式查找一列中的重复值并且在另一列里面列出来...
  8. 剑指offer面试题29. 顺时针打印矩阵(边界问题)
  9. Redis教程:基础知识
  10. C++中map的用法详解