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

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

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

Sample Output

2
3

Source

 
尺取法-->挑战详解
我们设以a[s] 开始总和最初大于S时的连续子序列为a[s] + ... + a[t-1],这时a[s+1] + ... + a[t-2] < a[s] + ... + a[t-2] < S,所以从a[s+1]开始总和最初超过S的连续子序列如果是a[s+1] + ... + a[t'-1]的话,则必然有t <= t'。
算法思路:
(1)初始化s = t = sum = 0.
(2)只要依然有sum < S,就不断将sum增加a[t],并将t增加一。
(3)如果(2)中无法满足sum >= S则终止。否则 更新结果ans = min(ans, t-s)。
(4)将sum减去a[t],s增加1然后回到(2)。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int a[];
int main()
{
int t;
scanf_s("%d", &t);
while (t--)
{
int n, s;
cin >> n >> s;
int i;
for (i = ; i <= n; i++)
{
scanf_s("%d", &a[i]);
}
int st = ;
int ed = ;
int sum = ;
int ans = inf;
while ()
{
while (ed <=n&&sum < s)
sum += a[ed++];
if (sum < s) break;
ans = min(ans, ed - st);
sum -= a[st++];
}
if (ans == inf) printf("%d\n", );
else printf("%d\n", ans);
}
return ;
}

 

POJ 3061 Subsequence(尺取法)的更多相关文章

  1. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  2. POJ 3061 Subsequence 尺取法

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

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

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

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

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

  5. POJ 3061 Subsequence 尺取

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

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

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

  7. POJ 3061 Subsequence ( 尺取法)

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

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

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

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

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

随机推荐

  1. Python中的数据结构 --- 列表(list)

      列表(list)是Python中最基本的.最常用的数据结构(相当于C语言中的数组,与C语言不同的是:列表可以存储任意数据类型的数据). 列表中的每一个元素分配一个索引号,且索引的下标是从0开始. ...

  2. Git 之 问题集锦

    准备:远程仓库名:origin     远程分支:master.tt      本地分支:master.test 1. error: src refspec *** does not match an ...

  3. GPU编程自学5 —— 线程协作

    深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...

  4. 《Drools7.0.0.Final规则引擎教程》第4章 4.6 结果条件

    结果条件 在Java中,如果有重复的代码我们会考虑进行重构,抽取公共方法或继承父类,以减少相同的代码在多处出现,达到代码的最优管理和不必要的麻烦.Drools同样提供了类似的功能.下面我们以实例来逐步 ...

  5. Eclipse下搭建SWT与Swing图形界面开发环境

    一.SWT与Swing介绍 SWT(StandardWidget Toolkit)则是由Eclipse项目组织开发的一套完整的图形界面开发包,虽然当初仅仅是IBM为了编写Eclipse的IDE环境才编 ...

  6. ss-libev 源码解析udp篇 (3)

    本篇分析server_recv_cb,这个是udp转发中最重要的函数. server_recv_cb: 当ss-local或ss-server接收到来自前端的udp数据包时调用.这个函数代码比较多,除 ...

  7. Emgu cv 学习笔记

    http://www.cnblogs.com/CoverCat/p/5003363.html emgu中imagebox与picturebox imagebox 是emgu   设置好厚,新出现的控件 ...

  8. HDU 1043

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://www.cnblogs.com/goodness/archive/2010/05/04/17 ...

  9. HDU 2268

    http://acm.hdu.edu.cn/showproblem.php?pid=2268 小学四年级应用题,让我找回了儿时的快乐... #include <iostream> #inc ...

  10. c语言第4次作业

    题目7-2九九乘法表 1.代码: #include<stdio.h> int main() { int N, i, j, q; scanf("%d",&N); ...