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

 
题意:给你n个数,和S,要求在n个数中找一个区间,使区间和大于S,且这个区间长度最小。
题解:经典简单题目,可以使用二分法或尺取法。
一、二分
计算前缀和,枚举每个元素,作为区间的起始点,并在之后的前缀数组中进行二分法,当s[mid]-s[i]>S时,左区间查找,反之右区间,记录最小的mid-i+1;
 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; const int INF = ;
int a[];
int main()
{
int S, t, n;
int T, mi;
int l, r, mid;
while(cin >> T)
{
while(T--)
{
cin >> n >> S;
for(int i = ; i <= n; i++)
{
scanf("%d", &t);
if(i == )
a[i] = t;
else
a[i] = a[i-] + t;
} a[] = ;
mi = INF;
for(int i = ; i <= n; i++)
{
l = i;
r = n;
while(l <= r)
{
mid = (l + r)/;
if( a[mid] - a[i-] >= S)
{
if( mi > mid-i+ )
mi = mid - i + ;
r = mid - ;
}
else
{
l = mid + ;
}
}
}
if(mi == INF)
printf("0\n");
else printf("%d\n", mi); }
}
return ;
}
二、尺取
先取前x个数(r++),直到大于S,减去该区间最前面的一个数(收缩 l++),再次判断是否大于S,重复操作,直至t==n或 取得的区间无法大于S 停止。
 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; int a[];
const int INF = ;
int main()
{
int T, n, S, sum;
int l, r, mi; while(cin >> T)
{
while(T--)
{
cin >> n >> S;
for(int i = ; i < n; i++)
{
scanf("%d", a + i);
} l = r = sum = ;
mi = INF;
for(;;)
{
while(r < n && sum < S)
{
sum += a[r++];
}
if( sum < S)
break;
else
{
mi = min(mi, r - l);
sum -= a[l++];
}
} if(mi == INF)
printf("0\n");
else printf("%d\n", mi);
}
}
return ;
}
 
 
 

POJ3061 Subsequence 尺取or二分的更多相关文章

  1. POJ 3061 Subsequence 尺取

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

  2. POJ3061——Subsequence(尺取法)

    Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...

  3. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  4. poj3061 Subsequence(尺取)

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

  5. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  6. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  7. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  8. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  9. 1686 第K大区间(尺取+二分)

    1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...

随机推荐

  1. 本周实验PSP0 过程文档

    2016-03-12 项目总结: 日期\学习时间 听课 编写程序 阅读相关书籍 日总计 周一 110 0 30 140 周二 0 30 30 60 周三 0 40 0 40 周四 110 20 30 ...

  2. 2018软工实践—Alpha冲刺(4)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前后端接口的开发 测试项目运行的服务器环 ...

  3. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  4. Windows下基于http的git服务器搭建-gitstack

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Windows下基于http的git服务器搭建-gitstack     本文地址:http: ...

  5. delphi完美经典--第十八章

    第18章数据感知组件 一.TDBText组件 用来以只读.一次一条记录的方式,显示DataSet中的某一字段值.因同样继承自TCustomLabel,TDBText组件除了数据感知功能外,与标准组件T ...

  6. js中关于array的常用方法

    最近总结了一些关于array中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. 第 ...

  7. bzoj4639 博士的选取器

    题意 给出一个长度为n的正整数序列,要求把它划分成若干个连续的区间,使得每个区间的数字之和都不超过给定的lim.最后的代价等于每个区间的最大值之和.求最小代价.n<=300000 分析 定义f[ ...

  8. 【Java】Java CSV操作代码

    CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件.在 CSV文件中,数据“栏”以逗号分隔,可允许程序通 ...

  9. 关于监听与控制设备旋转全解析(UIDeviceOrientationDidChangeNotification)

    一类情况: 初始化app的方向,比如只支持横屏或者竖屏.下面举例只支持竖屏的案例 在app的属性里面手动设置 上面标注了该app支持的方向种类,要是在app里支持Portrait方向,还需要添加以下代 ...

  10. 【BZOJ3243】【NOI2013】向量内积(矩阵,数论)

    [BZOJ3243][NOI2013]向量内积(矩阵,数论) 题面 BZOJ 题解 这题好神仙. 首先\(60\)分直接是送的.加点随机之类的可以多得点分. 考虑正解. 我们先考虑一下暴力. 我们把\ ...