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. (一)easyUI之第一个demo

    一.下载 官网下载 : http://www.jeasyui.net/download/   同时并下载官方中文API文档. 解压后的目录结构: 二.第一个demo 1      新建工程并导入包 2 ...

  2. Angularjs 省市区级联

    Json 地区文件:http://blog.csdn.net/youshi520000/article/details/70808580 angularjs angular.module('app') ...

  3. Java Web 深入分析(8) Servlet工作原理解析

    Servlet Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态We ...

  4. spring将service添加事务管理,在applicationContext.xml文件中的设置

    在applicationContext.xml文件中的设置为: <beans> <bean id="sessionFactory" class="org ...

  5. nodejs实现邮件发送

    需要安装的node模块 nodemailer 新建项目目录 mail-test 进入这个项目里使用终端初始化package.json(npm init) 安装express和nodemailer并保存 ...

  6. 解决window 12 service 不能调用excel ,报"System.Runtime.InteropServices.COMException (0x800A03EC)

    Step1: 运行comexp.msc -32 注意因为excel 是32 位的,所以这里用的32位的操作, 如用64位的操作命令:dcomcnfg.exe,将找不到excel com 进入compo ...

  7. jvm系列(七):jvm调优

    转自:https://www.cnblogs.com/ityouknow/p/6437037.html 16年的时候花了一些时间整理了一些关于jvm的介绍文章,到现在回顾起来还是一些还没有补充全面,其 ...

  8. vue v-cloak 指令 处理页面显示源码

    有时候页面会先出现源码,再显示解析的内容.可以通过v-cloak解决 v-cloak 这个指令会作为元素的一个属性一直保持到vue实例编译结束,即解析后移除此指令. /* 含有v-cloak的html ...

  9. 本地开发的jar包放到本地maven仓库

    mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\taobao-sdk-java-1455552377940-2016060 ...

  10. webpack中环境变量的使用方法

    这节课讲解一下,在webpack打包过程中,怎么去使用一些环境变量. 首先我有一个打包配置的三个文件 "scripts": { "dev-build": &qu ...