LA 3029

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.

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

思路:这道题主要难度在于时间,数据范围是十的五次方,如果采用传统的O(n^2)算法恐怕会力不从心,因此要想到改变枚举方式,只枚举子序列的终点?

使用前缀合sum[i],那么最开始是设j=0,那么开始枚举sum[i]-sum[j],如果找到一个符合条件的子序列,就尝试逐渐增加j,最后也无需把j置零,因为sum数列是递增的.如果前面有sum[i]-sum[j]符合条件,那么任何大于i的k都符合sum[k]-sum[j]>=s

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=1e5+;
int sum[maxn];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
sum[]=;
int a;
for(int i=;i<=n;i++) {scanf("%d",&a);sum[i]=sum[i-]+a;}
int j=;
int minx=1e9;
for(int i=;i<=n;i++)
{
if(sum[i]-sum[j]<k) continue;
while(sum[i]-sum[j]>=k) j++;
minx=min(i-j+,minx);
}
printf("%d\n",minx==1e9?:minx);
}
return ;
}

这段程序的时间复杂度为O(n),因为外层虽然i从1循环到n,而对于每个i都不可能有从1到n的j循环,顶多就是所有i内的j循环加起来等于n次而已

还可以使用二分法查找合适的j,时间复杂度为O(nlogn)

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e5+;
int sum[maxn];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
sum[]=;
int a;
for(int i=;i<=n;i++) {scanf("%d",&a);sum[i]=sum[i-]+a;}
int j=;
int minx=1e9;
for(int i=;i<=n;i++)
{
j=lower_bound(sum,sum+i,sum[i]-k)-sum;
if(j>) minx=min(minx,i-j+);
}
printf("%d\n",minx==1e9?:minx);
}
return ;
}

PS:使用lower_bound()进行查找,如果所查找的数比数列中所有的数都要小,就会返回数组头位置的前一位,如果查找的数比数列中所有的数都要大,那么就会返回数组尾的后一位,原因想一想二分法的原理就知道了

LA 3029 Subsequence的更多相关文章

  1. LA 3029 City Game

    LA 3029 求最大子矩阵问题,主要考虑枚举方法,直接枚举肯定是不行的,因为一个大矩阵的子矩阵个数是指数级的,因此应该考虑先进行枚举前的扫描工作. 使用left,right,up数组分别记录从i,j ...

  2. LA 2678 Subsequence(二分查找)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. LA 2678 Subsequence

    有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S ...

  4. LA 3029 - City Game (简单扫描线)

    题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 ...

  5. UVa LA 3029 City Game 状态拆分,最大子矩阵O(n2) 难度:2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. LA 2678 – Subsequence

    看到限时3S,自己写了一个二重循环的,然后华丽的 TLE...T T 瞄了瞄书上,作者的思路果然是很好.膜拜中. 他只枚举了终点,然后用二分查找. 用到了lower_bound函数,这个lower_b ...

  7. 【巧妙预处理系列】【UVA1330】City game

    最大子矩阵(City Game, SEERC 2004, LA 3029) 给定一个m×n的矩阵,其中一些格子是空地(F),其他是障碍(R).找出一个全部由F组成的面积最大的子矩阵,输出其面积乘以3后 ...

  8. HDU 1159:Common Subsequence(LCS模板)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

随机推荐

  1. 10. Extjs Button控件的handler配置项和click事件

    转自:https://blog.csdn.net/lishk314/article/details/41541797 查看API: click( this, e, eOpts ) Fires when ...

  2. Ubuntu12.04安装JDK(jdk-6u45-linux-x64.bin)(转载)

    首先下载jdk-6u45-linux-x64.bin, 如果是32bit系统下载相应的i386即可. 更改文件权限-> chmod a+x jdk-6u45-linux-x64.bin. 创建目 ...

  3. vue 加载文件,省略后缀后的加载顺序

    Vue使用import ... from ...来导入组件,库,变量等.而from后的来源可以是js,vue,json.这个是在webpack.base.conf.js中设置的: module.exp ...

  4. bzoj 4031: [HEOI2015]小Z的房间【矩阵树定理】

    是板子题,因为mod不是质数所以需要辗转相除然而并不知道为啥 高斯消元部分还不知道原理呢--先无脑背过的 #include<iostream> #include<cstdio> ...

  5. P4576 [CQOI2013]棋盘游戏

    传送门 很显然,除非白子和黑子相邻,否则必然是黑子获胜虽然我并没有看出来 那么现在对黑子来说它要尽可能快的赢,对白子它要多苟一会儿 然后就是这个叫做对抗搜索的东西了 //minamoto #inclu ...

  6. springboot(三)配置日志

    github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-serviceSLF ...

  7. vs2017 + miniUI + dapper 添加、修改、删除、查询操作

    A.数据库表引用先前建立的company 公司信息表. B.建立文件: views > Home > Company.cshtml(新建文件) ,代码如下(直接复制即可) @{ Layou ...

  8. 乐搏讲自动化测试-python语言特点及优缺点(5)

    相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...

  9. Beyond Compare 激活解决办法

    问题: 当你使用过一段时间后会提示有问题,需要激活或者什么. 解决办法: 找到这个路径并删除其下Beyond Compare 3文件夹即可正常使用. C:\Users\******\AppData\R ...

  10. java dom4j xml生成,解析

    1. 用Java代码生成xml文档 package com.test.dom; import java.io.FileOutputStream; import java.io.IOException; ...