Poj 3061 Subsequence(二分+前缀和)
Subsequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12333 Accepted: 5178
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
Southeastern Europe 2006
/*
题目大意:
要求n个数总和不小于s的连续子序列的长度的最小值.
二分+前缀和查询.
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
using namespace std;
int n,m,s[MAXN],sum[MAXN];
bool check(int x)
{
for(int i=1;i<=n-x+1;i++){
if(sum[x+i-1]-sum[i-1]>=m) return true;
}
return false;
}
int erfen(int l,int r)
{
int mid,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&s[i]),sum[i]=s[i]+sum[i-1];
printf("%d\n",erfen(0,n+1));
}
return 0;
}
Poj 3061 Subsequence(二分+前缀和)的更多相关文章
- poj 3061 Subsequence 二分 前缀和 双指针
地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence 二分或者尺取法
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...
- POJ 3061 Subsequence ( 二分 || 尺取法 )
题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...
- POJ 3061 Subsequence 二分查找
题目大意:给出长度为n的一个序列,给出一个数字S,求长度最短的序列和大于等于S的连续子序列,输出该长度,如果没有答案输出0. 题目思路:看数据范围,这道题就是卡时间的.我们可以用sum[i]记录前i项 ...
- poj 3061 Subsequence
题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...
- POJ 3061 Subsequence【二分答案】||【尺取法】
<题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...
- POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence(Two Pointers)
[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子 ...
随机推荐
- 活用maven使web.xml可以用maven变量
活用maven使web.xml可以用maven变量 废话不多说,直接上代码 <build> <finalName>${finalWarName}</finalName&g ...
- HW1.1
public class Solution { public static void main(String[] args) { System.out.println("Welcome to ...
- storm出现的背景
storm出现的背景 互联网从诞生的第一时间起,对世界的最大改变就是让信息能够实时交互,从而大大加速了各个环节的效率.正因为大家有对信息实时响应.实时交互的需求,所以软件行业除了个人操作系统之外,数 ...
- 如何自己编译生成Eclipse插件,如hadoop-eclipse-plugin-2.2.0.jar
如何自己编译生成Eclipse插件,如hadoop-eclipse-plugin-2.2.0.jar http://cxshun.iteye.com/blog/1876366 大数据是当今的一个热门话 ...
- Redis 数据库结构设计
Redis设计参考资料: http://my.oschina.net/fsmwhx/blog/152130 http://my.oschina.net/1123581321/blog/164288 h ...
- Linux学习笔记——软件包管理
1.RPM包的管理 RPM软件包的一个例子: sudo-1.7.2p1-5.el5.i386.rpm 这个rpm包名包括软件名sudo,版本号1.7.2p1,发行号5.el5,和硬件平台i386 (1 ...
- SGU107——987654321 problem
For given number N you must output amount of N-digit numbers, such, that last digits of their square ...
- 如何通过进程名获取进程ID
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.
- Mybatis上路_06-使用Java自动生成
目录[-] 1.编写Generator执行配置文件: 2.在MyEclipse中建空web项目: 3.编写并执行Java程序: 4.查看并修改生成的文件: 5.测试,使用生成的文件查询: 1)导入My ...
- 高亮选中MEMO某一行
选中第5行 //转到指定行并选中这行的文本 procedure SelectLine(Memo1: TMemo; ln: Integer); begin Memo1.SelStart := SendM ...