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

Many test cases will be given. 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 there isn't such a subsequence, print 0 on a line by itself.

Sample Input

10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

Sample Output

2
3

n个正整数组成一个序列。给定整数S,求长度最短的连续序列,使它们的和大于或等于S

【输入格式】

输入包含多组数据。每组数据的第一行为整数nS(10<n≤100 000,S<109);第二行为n个正整数,均不超过10 000。输入结束标志为文件结束符(EOF)。

【输出格式】

对于每组数据,输出满足条件的最短序列的长度。如果不存在,输出0。

二分答案(即序列长度)+前缀和可以轻松搞定

复杂度为O(n*logn)

(这个方法可以解决数可能为负数的数据,所以也是一种值得记忆的方法)

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int n,S;
int A[100010];
int ans;
int OK(int m)
{
if(S==0) return 1;
int temp=0;
for(int i=1;i<=n-(m-1);i++)
{
temp=A[i+(m-1)]-A[i-1];
if(S<=temp) return 1;
}
return 0;
}
void solve()
{
ans=0;
int l=1,r=n;
int m=(l+r)/2;
while(l<r)
{
if(OK(m)) r=m;
else l=m+1;
m=(l+r)/2;
}
ans=m;
}
void input()
{
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
A[i]=A[i-1]+A[i];
}
}
int main()
{
while(scanf("%d%d",&n,&S)!=EOF)
{
input();
solve();
if(ans!=n)
printf("%d\n",ans);
else if(A[n]<S)
{
printf("0\n");
}
else printf("%d\n",n);
}
return 0;
}

【二分答案nlogn/标解O(n)】【UVA1121】Subsequence的更多相关文章

  1. 3月28日考试 题解(二分答案+树形DP+数学(高精))

    前言:考试挂了很多分,难受…… --------------------- T1:防御 题意简述:给一条长度为$n$的序列,第$i$个数的值为$a[i]$.现让你将序列分成$m$段,且让和最小的一段尽 ...

  2. [USACO]地震 (二分答案+最优比率生成树详解)

    题面:[USACO 2001 OPEN]地震 题目描述: 一场地震把约翰家的牧场摧毁了, 坚强的约翰决心重建家园. 约翰已经重建了N个牧场,现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发 ...

  3. P3105 [USACO14OPEN]公平的摄影(正解是乱搞,我却二分了)(+二分答案总结)

    照例化简题意: 给定一个01区间,可以把0改成1,问其中最长的01数量相等的区间长度. 额很容易想到前缀和,把w弄成1,h弄成-1,然后求前缀和,然后乱搞就行了. 但是一直不太会乱搞的我却直接想到了二 ...

  4. [NOIP2011] 聪明的质检员(二分答案)

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...

  5. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  6. BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心

    题目大意:给定一棵树,有一些点是关键点,要求选择不超过mm个点.使得全部关键点到近期的选择的点距离最大值最小 二分答案,问题转化为: 给定一棵树,有一些点是关键点,要求选择最少的点使得每一个关键点到选 ...

  7. 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑

    Assemble Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3171   Accepted: 1013 Descript ...

  8. Codeforces 700A As Fast As Possible(二分答案)

    [题目链接] http://codeforces.com/problemset/problem/700/A [题目大意] 有一辆限载k人速度为v2的车,n个步行速度均为v1的人要通过一段长度为l的距离 ...

  9. [BZOJ 2500]幸福的道路 树形dp+单调队列+二分答案

    考试的时候打了个树链剖分,而且还审错题了,以为是每天找所有点的最长路,原来是每天起点的树上最长路径再搞事情.. 先用dfs处理出来每个节点以他为根的子树的最长链和次长链.(后面会用到) 然后用类似dp ...

随机推荐

  1. 如何在WP8模拟器中连接本地的web服务

    这个问题困扰了很久,查找答案一度找偏方向. 其实连接web服务对于wp7不是问题,因为wp7使用的网络就是本机的网络,但是到了wp8模拟器,纯粹的虚拟机,独立的设备,也就有了自己的网络连接,要当做虚拟 ...

  2. 你所不了解的css选择器

    我们目前接触到的选择器:.class   #id  div  ...... 不了解的选择器:a>b   a+b [a~=b] [a|=b]......   一下说举5 6 7 8为css3中的定 ...

  3. @synthesize

    @synthesize 相当于把属性当成成员变量来用,不用再写self.属性@synthesize myButton; 这样写了之后,那么编译器会自动生成myButton的实例变量,以及相应的gett ...

  4. 对boost::shared_from_this的进一步封装

    对boost::shared_from_this的进一步封装 熟悉异步编程的同学可能会对boost::shared_from_this有所了解.我们在传入回调的时候,通常会想要其带上当前类对象的上下文 ...

  5. CSS3实现时间表

    一直想学CSS3,今天别的论坛看到的例子,挺好,mark起来以后自己看. <!doctype html> <html> <head> <meta charse ...

  6. Laravel 模板引擎Blade中标签详细介绍

    这篇文章主要介绍了Laravel模板引擎Blade中section的一些标签的区别介绍,需要的朋友可以来看看. Laravel 框架中的Blade模板引擎很好用,但是官方文档介绍的并不详细,我接下来将 ...

  7. DataSnap

    一. DataSnap REST - http://docwiki.embarcadero.com/RADStudio/Berlin/en/DataSnap_REST 1. URI Mapping: ...

  8. Apple Catching(POJ 2385)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9978   Accepted: 4839 De ...

  9. backboneJS 使用心得(2)view和model事件的多次绑定(转载)

    一,new 一个新的View对象时,最好把,el参数传进去:而不是在view里面操作view的外部节点. 比如: 外部调用 var view=new BankboneView({el,$('body' ...

  10. 运行时数据区即内存分配管理——JVM之六

    内存分配结构,请参考: http://iamzhongyong.iteye.com/blog/1333100