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. LeetCode OJ:Valid Sudoku(有效数独问题)

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  2. SpringAnnotation之配置AnnotationXML文件

    配置Annotation的环境:只需修改applicationContext.xml文件即可 1 2 3 4 5 6 7 8 9 10 11 <?xml version="1.0&qu ...

  3. Java并发编程之读写锁

    读写锁维护了一对相关的锁,一个用于只读操作,一个用于写入操作.只要没有writer,读取锁可以由多个reader线程同时保持.写入锁是独占的. 可重入读写锁 ReentrantReadWriteLoc ...

  4. Android 开发 Tip 17 -- 为什么getBackground().setAlpha(); 会影响别的控件?

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/75670018 http://www.jb51.net/article/110035.h ...

  5. JDK 1.8之 HashMap 源码分析

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/75579654 构造函数 Node hash put treeifyBin get re ...

  6. Jira简单使用操作指引20150605

    1.选择项目 2.点击[问题]——>[所有问题] 3.选择状态(一般开发关注[新增.处理中],测试关注[已解决.已作废]) 4.选择[more],勾选[解决版本].[影响版本].[解决人],我们 ...

  7. 数据链路层、ARP/RARP、ICMP、ping和traceroute

    互联网基础:   数据链路层:RFC文档:894/1042/1340 为IP模块发送和接受IP数据报 为ARP模块发送ARP请求和接收ARP应答 为RARP模块发送RARP请求和接收RARP应答   ...

  8. SQL基础四(例子)

    ------------------------------------------------ --分别创建student/course/score表 Create table student ( ...

  9. c++语言第二次作业

    一题目7-1统计学生成绩 1实验代码 #include<stdio.h> int main(void) { int i,n,grade,A,B,C,D,E; A=B=C=D=E=; sca ...

  10. Uncaught TypeError: jQuery.i18n.browserLang is not a function

    /********************************************************************* * Uncaught TypeError: jQuery. ...