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. C++ 报错 R6030 CRT not initialized

    昨天,在写一个算法的时候,报错R6030 CRT not initialized. 认真检查发现,是出了比较低级的错误. 一. 会出错的代码,编译的时候不会报错,执行过程中报R6030 CRT not ...

  2. 给一个int型整数,如何将这个整数的奇偶位互换

    题目: 假设一个8为整数是(10101100)b那么奇偶互换之后就是(01011100)b.假设机器是32位的 注意: 8位中最低位开始数,最低位是第0位,是偶数为,次低位时第1位,是偶数位. 做法: ...

  3. Unity之Bmob云存储一

    无论我们做软件还是做游戏,少不了的就是和数据打交道,对于要保存到本地的数据,我们可以采用的载体太多了.例如:txt,Xml,Sqlite,SqlServer,Mysql等等,具体使用什么那就视情况而定 ...

  4. [Spring入门学习笔记][maven]

    什么是maven? 我的理解: 一个项目有一大堆依赖包的时候,没必要下下来,可以利用maven中的pom.xml 指定需要那些依赖包,让maven去本地中央库(如果没找到)->网上仓库库帮你调用 ...

  5. java编程中的Java.Lang.Math类

    1. Math.PI  :表示的是圆周率常量: 2.Math.E    :表示的是普通常量(e): 3.abs()方法: 表示取绝对值 eg1: int x = Math.abs(50L);     ...

  6. CCS v5 无法启动解决办法及Launchpad仿真器电脑无法识别解决方法

    安装ccs_setup_5.1.1.00028.exe后(无论是自己装eclipse还是在原来的基础上安装eclipse的插件),ccs5的应用无法打开,错误为:An error has occurr ...

  7. css_day5

  8. 一个Demo就懂的Angular之directive

    <body> <div ng-controller="myCtrl"> <hello-word></hello-word> < ...

  9. jquery模拟checkbox效果,以及background-size在jquery中的使用。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. Margin是什么?

    Margin是什么 CSS 边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距属性同时改变所有的外边距.——W3School 边界,元素周围生 ...