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. CDN加速的实现 --- varnish

    一.什么是CDN cdn全称为内容分发网络(Content Delivery Network).基本思想是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,是内容传输地更快.更稳定.通过在 ...

  2. ubuntu16安装git

    执行一条命令就可以: sudo apt-get install git

  3. gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea

    gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea ...

  4. topcoder WCNMLGB

    点开题->断开连接->再也上不去->我玩NM

  5. NSLog 打印出方法函数,行数,内容

    #if DEBUG #define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", ...

  6. C++语言的url encode 和decode

    std::string UrlEncode(const std::string& szToEncode) { std::string src = szToEncode; char hex[] ...

  7. 关于HTTPS理论和实践

    Summary: HTTP 通信时,可能信息被监听.服务器或客户端身份伪装等安全问题,HTTPS 则能有效解决这些问题. 原始的HTTP连接的时候,因服务器与用户之间是直接进行明文传输,导致用户面临着 ...

  8. Linux文件传输scp和rsync断点续传

    scp 是通过ssh协议传输数据,如果是想传输一个很大的数据,这几天我就遇到过,一个tar包,400G左右,通过scp拷贝时,有可能遇到服务器断电什么的悲剧问题,那么类似scp拷贝的另一个命令 rsy ...

  9. LOJ2323. 「清华集训 2017」小 Y 和地铁 【搜索】【思维】【好】

    LINK 思路 首先如果直接算每一个段有三个决策 左/右 上/下 跨不跨过端点 这样的复杂度是\((2^3)^{22}\),显然是无法接受的 然后考虑怎么优化这个东西 首先左右这个决策是没有意义的 因 ...

  10. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...