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. 团队介绍 you i

    我们团队一共四个人,我们足够了解对方的优缺点,能够很好的进行交流沟通.对于一些问题也能有好的方法去解决,我做事情比较讲究高效和尽可能的完美,或者说要做到我自己觉得完美,才会停下来.对于一件事情,我有自 ...

  2. Alpha 冲刺(6/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...

  3. struts2--上传总结(限制大小和类型 非法上传的跳转)

    网上有很多版本,鉴于实践出真知的态度 我自己探索了一番 struts版本:2.3.16 限制大小: struts2默认是2M 所以如果要扩大大小限制,应该先配一个全局struts2最大上限 <c ...

  4. 从入门到不放弃——OO第一次作业总结

    写在最前面: 我是一个这学期之前从未接触过java的小白,对面向对象的理解可能也只是停留在大一python讲过几节课的面向对象.幸运的是,可能由于前三次作业难度还是较低,并未给我造成太大的困难,接下来 ...

  5. 设计模式PHP篇(三)————适配器模式

    简单的适配器模式: interface Adaptor { public function read(); public function write(); } class File implemen ...

  6. (转)ActiveMQ的重连机制

    花了一天的时间,终于搞明白了我的疑问. failover://(tcp://localhost:6168)?randomize=false&initialReconnectDelay=100& ...

  7. 微信小程序组件 加减号弹出框

    <!-- 点击立即抢拼弹出框 --> <view class='add-rob' bindtap="setModalStatus" data-status=&qu ...

  8. free word online

    free word online https://office.live.com/start/Word.aspx https://www.lifewire.com/free-online-word-p ...

  9. 【Linux笔记】阿里云服务器被暴力破解

    一.关于暴力破解 前几天新购进了一台阿里云服务器,使用过程中时常会收到“主机被暴力破解”的警告,警告信息如下: 云盾用户您好!您的主机:... 正在被暴力破解,系统已自动启动破解保护.详情请登录htt ...

  10. BZOJ3594 SCOI2014方伯伯的玉米田(动态规划+树状数组)

    可以发现每次都对后缀+1是不会劣的.考虑dp:设f[i][j]为前i个数一共+1了j次时包含第i个数的LIS长度.则f[i][j]=max(f[i][j-1],f[k][l]+1) (k<i,l ...