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. 404 Note Found· 第七次作业 - 需求分析报告

    目录 组队后的团队项目的整体计划安排 项目logo及思维导图 项目logo 思维导图 产品思维导图 产品思维导图-引导 产品思维导图-后端数据处理.存储 产品思维导图-短信识别 产品思维导图-智能分析 ...

  2. 技嘉主板+AMD CPU开启CPU虚拟化方法

    硬件环境:技嘉AB350+AMD Ryzen 5 1600X 由于安装虚拟机的需要,所以要开启CPU的虚拟化. 首先进入BIOS. 然后如图:(M.I.T-高级频率设定-CPU超频进阶设置-SVM M ...

  3. SpringMVC项目中获取所有URL到Controller Method的映射

    Spring是一个很好很强大的开源框架,它就像是一个容器,为我们提供了各种Bean组件和服务.对于MVC这部分而言,它里面实现了从Url请求映射控制器方法的逻辑处理,在我们平时的开发工作中并不需要太多 ...

  4. Struts2(五)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.文件上传 A.单文件上传 <body> <form action="${pageContext.requ ...

  5. vue-cli3使用 DllPlugin 实现预编译,提升构建速度

    在项目打包上有两个目标:减少打包代码体积和加快打包速度 1. 减少打包体积: (1)对于用的比较少的库,可以去掉(我去掉了jquery以及lodash),用到的地方,参考源码自己写 (2)非用不可的又 ...

  6. ViewPager、Fragment、Matrix综合使用实现Tab滑页效果

    原文地址:http://www.cnblogs.com/kross/p/3372987.html 我们实现一个上面是一个可以左右滑动的页面,下面是三个可点击切换的tab按钮,tab按钮上还有一个激活条 ...

  7. sql 两列数据交换

    MSSQL的处理方法   update table1 set field_1 = field_2, field_2 = field_1 可是MySQL就不能这样写,不然一列会覆盖另一列记录   MyS ...

  8. jumpserver的安装部署

    废话不说直接安装 1:安装数据库 这里是提前安装,也可以不安装,在安装jumpserver主程序的时候,他会询问你是否安装 yum -y install ncurses-devel cmake ech ...

  9. bzoj3545-bzoj3551-Peaks

    题意 给出一个图,边有边权,点有点权,每次询问一个点 \(x\) 只走边权小于等于 \(d\) 的边能到达的点中点权第 \(k\) 大. 强制在线,\(n\le 10^5,m,q\le 5\times ...

  10. 洛谷 P1352 没有上司的舞会

    树形动规入门题 先放题面 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都 ...