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. 破解google翻译API全过程

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...

  2. Linux:join命令详解

    join 处理两个文件之间的数据,并且将两个文件中有相同的数据的那一行加在一起 语法 join(选项)(file1 file2) 选项 -a<1或2>:除了显示原来的输出内容之外,还显示指 ...

  3. 网络请求工具 cURL

    网络请求工具 cURL 2017-08-16 cRUL 是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在“标准输出”(stdout)上面. 查看网页源码 1 $ curl baidu. ...

  4. 【PL/SQL编程】条件语句

    1. if...then语句 if <condition_expression> then plsql_sentence; end if; declare -- Local variabl ...

  5. 微信红包算法TEST

    1.基本算法 设定总金额为10元,有N个人随机领取:N=1 则红包金额=X元: N=2 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数 第二个红包=10-第一个红包金 ...

  6. three.js入门系列之材质

    一.基础网孔材料 MeshBasicMaterial 图示(光源是(0,1,0)处的点光源): 二.深度网孔材料 MeshDepthMaterial (由于只是改了材料名,代码将不重复贴出) 在这里, ...

  7. Leetcode 894. All Possible Full Binary Trees

    递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...

  8. SSH项目搭建(一)

    本章不讲项目,讲一下SSH的下载 一.Struts2 1.通过百度进入官网 2.点击Download>>>>Full Releases>>>>你想要的版 ...

  9. sqlserver 执行脚本报内存溢出的处理方式

    用微软自带的sqlcmd工具,可以导入执行.以SQL Server 2008R2版本为例: 第一步:Win+R 键入:cmd 命令,开启命令行工具: 第二步:键入:cd C:\Program File ...

  10. 模块(Modules)

    一.引入模块 模块:当编写更大的应用程序时,所有的代码肯定会分成多个文件,这样便于维护,另外已经编写好的函数和对象在被多个程序中使用时,不用把函数和对象拷贝到每个程序中. 模块支持以上功能,在Pyth ...