题干:

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

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

Sample Output

2
3

解题报告:

这题貌似二分或者尺取法都可以。

AC代码:(二分)

#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;ll a[100005];
ll er[100005];
int main()
{int t;ll n,s;ll l,r;int minn;cin >> t;while(t--){memset(a,0,sizeof(a));cin >> n >> s;for(int i=1;i<=n;i++){scanf("%lld",&a[i]);a[i]=a[i-1]+a[i];er[i]=n; }
//      cout << a[n] << endl;if(a[n]<s){cout << 0 << endl;continue;}for(ll i=1;a[n]-a[i-1]>=s;i++){er[i]=lower_bound(a+i+1,a+n+1,a[i]+s)-(a+i/*-1*/);} cout << *min_element(er+1,er+n+1) << endl;}return 0 ;
}

尺取法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;int t;
int n,S;
int a[100005];
int sum[100005];int main(){cin >> t;while (t--){cin >> n >> S;for (int i=0;i<n;i++){cin >> a[i];}int ans=n+1;int s=0,t=0,sum=0;while (1){while (t<n&&sum<S){sum+=a[t++];}if (sum<S) break;ans=min(ans,t-s);sum-=a[s++];}if (ans>n) ans=0;cout << ans << endl;}}

*【POJ - 3061】 Subsequence (尺取或二分)相关推荐

  1. poj 3061 Subsequence 尺取法

    题目描述 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0 解题思路: 取尺法: 取尺法通常是指对数组保存一对下标(起点和重点 ...

  2. 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

    题干: Just days before the JCPC, your internet service went down. You decided to continue your trainin ...

  3. 【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

    题干: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the l ...

  4. Bound Found POJ - 2566 (尺取+前缀和)

    题意 就是给一串序列 在给一个t 求一段区间的加和绝对值与t最小的值与区间左右端点是多少 思路 这道题一开始不知道如何去做 用尺取法求区间但是因为区间中存在负数没有单调性  没有特殊的特征 若对区间求 ...

  5. POJ 3061 -- Subsequence(二分)

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

  6. poj 3061(尺取法)

    从长为n的全为正数的数组a中,找到个数最少的连续子数列使其和>=m example: n=10,m=15,a[10]={5 1 3 5 10 7 4 9 2 8},结果为2 解题思路:昨天做了一 ...

  7. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  8. 尺取法 POJ 3601 Subsequence

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

  9. 【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)

    题干: An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., a ...

最新文章

  1. Linux I2C(一)之常用的几种实例化(i2c_client ) 【转】
  2. python【蓝桥杯vip练习题库】ALGO-75筛选号码(约瑟夫环)
  3. 、简述global关键字的作用_详解static inline关键字
  4. 减少主要GC暂停的频率
  5. 根据另外一个表来更新,增加字段
  6. 写题过程中碰见的小问题
  7. 织梦缩略图自动补齐绝对路径_[教程]织梦CMS缩略图和文章内容图片自动转化为带域名的绝对路径...
  8. Linux设备驱动模型-Kset
  9. 申请CSDN博客专家的成功历程
  10. linux客户端 手机,WAPI客户端在Linux系统中的实现
  11. 熟练计算机数字键盘,电脑键盘盲打及快速输入技巧【一】
  12. 超市便利店零售POS收银前台,好用的零售收银软件盘点机PDA收银机,批发销售出库单开单,超市零售批发进销存收银开单管理软件
  13. 【jetson nano】SD卡驱动挂载到nano板
  14. 【创作中心】自定义模板的使用
  15. LANDESK8.8版本操作说明书之核心服务器管理
  16. bmob android博客,(android)bmob中实现保存
  17. PHP面试高频算法面试题 -- 判断链表是否有环
  18. 2020年全新开放式学习资源
  19. 教科书般的亵渎(dfs)
  20. 操作系统期末习题考试习题解答题目一

热门文章

  1. [剑指offer][JAVA]面试题第[15]题[二进制中1的个数][位运算]
  2. HDU-4793 Collision 计算几何 解方程
  3. 会php学java入门要多久_php8(java入门要多久)
  4. ps 毛发 边缘_不会抠图怎么办?PS画笔绘制毛发技巧,抠图流程解析
  5. symfony api 错误响应_Postman for Mac(API管理开发软件)
  6. 郑州it java_郑州Java网站开发
  7. mysql skip_counter_mysql的三个故障解决小结
  8. figcaption html5,HTML5 figcaption 标签
  9. 粒子群 多目标 matlab_matlab 粒子群求解三角形垂心位置
  10. 四大开源分布式存储_ipfs分布式存储行业面临着四大主要风险,你知道是哪些吗?...