Problem

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

题解: 其实这题我感觉没有完全用到尺取的方法,可能思想有一点,就是从左端开始枚举,如果当前的和是小于m的,就让右端点右移,sum+=a[r],如果一但满足大于等于m,那么就计算一次ans,然后把左端点左移。重复上面直到遍历一遍就可以了。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int a[maxn];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d %d", &n,&m);
for(int i = 0; i < n; i ++)
scanf("%d",&a[i]);
int l = 0,r = 0,sum = 0,ans = n + 1;
while(1)
{
while(r<n&&sum<m)
{
sum=sum+a[r];
r ++;
}
if(sum<m)
{
break;
}
ans = min(r-l,ans);
sum=sum-a[l];
l++;
}
if(ans == n + 1)
{
printf("0\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}

Subsequence (POJ - 3061)(尺取思想)的更多相关文章

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

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

  2. B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值

    B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...

  3. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  4. POJ-3061 Subsequence 二分或尺取

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

  5. poi 3061 尺取例题1

    题目传送门/res tp poj 白书题 尺取法例题 #include<iostream> #include<algorithm> using namespace std; c ...

  6. Subsequence POJ - 3061

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

  7. Greedy:Subsequence(POJ 3061)

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

  8. POJ 3061 Subsequence 尺取

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

  9. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

随机推荐

  1. 如何使用JavaScript实现纯前端读取和导出excel文件(转)

    转自小茗同学博客:https://www.cnblogs.com/liuxianan/p/js-excel.html js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯 ...

  2. MarkdownPad2安装与破解-转载

    MarkdownPad安装包下载链接链接:https://pan.baidu.com/s/1o7c4W7C2d8zCPh5z7y4IvQ提取码:e4bf 下载解压之后,找要MarkdownPad2.e ...

  3. (五)Spring Boot之@RestController注解和ConfigurationProperties配置多个属性

    一.@RestController和@Controller的区别 @RestController注解相当于@ResponseBody + @Controller合在一起的作用. 如果只是使用@Rest ...

  4. 【原创】大叔经验分享(86)hive和mysql数据互导

    hive和mysql数据互导,首先想到的是sqoop,并且可以和调度框架(比如oozie等)配合配置定时任务,还有一种更简单的方式是通过spark-sql: CREATE OR REPLACE TEM ...

  5. Go part 5 结构体,方法与接收器

    结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...

  6. 在浏览器输入 URL 回车之后发生了什么

    注意:本文的步骤是建立在,请求的是一个简单的 HTTP 请求,没有 HTTPS.HTTP2.最简单的 DNS.没有代理.并且服务器没有任何问题的基础上. 大致流程 URL 解析 DNS 查询 TCP ...

  7. ASE —— 第一次结对作业

    问题定义 游戏规则: N个玩家,每人写一个或两个0~100之间的有理数 (不包括0或100),提交给服务器,服务器在当前回合结束时算出所有数字的平均值,然后乘以0.618,得到G值. 提交的数字最靠近 ...

  8. Java面试容器,collection,list,set

      1.容器指的是可以容纳其他对象的对象. 2.collection/set/list的联系和区别? (1)collection是Java集合顶级接口,存储一组不唯一,无序的对象: (2)list接口 ...

  9. CentOS linux7 设置开机启动服务

    常用命令 描述                                 旧命令  新命令 使服务自动启动          chkconfig --level 3 http on  syste ...

  10. Go语言——概念

    静态类型.动态类型.潜在类型 静态类型:指在变量声明中示出的那个类型.绝大多数类型都只有静态类型.唯独接口类型的变量例外,他除了拥有静态类型之外,还拥有动态类型. 动态类型:指在运行时与该变量绑定在一 ...