LA 3029 Subsequence

时间:2022-09-22 16:25:17

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&lpar;n2&rpar; 难度&colon;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&colon;Longest Match(最长公共子序列)&amp&semi;&amp&semi;HDU1458Common Subsequence &lpar; LCS)

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

随机推荐

  1. Genesis2000使用c&num;开发脚本

    这是我自学程序以来在博客园的第一篇博客,如有不好的地方请大家指正,谢谢! 这边文章的目的是给予那些在PCB使用Genesis2000程序脚本开发的人员提供.net平台下的开发方法. 目前genesis ...

  2. Linux high memory 学习总结

    在free命令中有个参数l,它表示 show detailed low and high memory statistics.其实最先是对High Memory总是为零有些不解(Linux是64为). ...

  3. 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法

    首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...

  4. Android kxml解析WBXML

     WAP Binary XML定义好XML片断表述出同步server地址.远程数据库名称.登录账号等等内容一.两种訪问方法: 眼下的kxml支持两种wap格式:WBXML/WML. 而有两种方法将解析 ...

  5. put a favicon for github pages

    put the picture "favicon.ico" in the root of your web page repo.then add the following lin ...

  6. 第44章:MongoDB-集群--Sharding&lpar;分片&rpar;--分片的片键选择

    ①片键选择的重要性 所谓片键,就是用来拆分数据的字段,通常为1-2个字段,由于片键一旦确定,并已经分片过后,基本上就不可能再修改片键了,因此初期设计和选择就非常重要了 ②片键规则 1:不可以是数组 2 ...

  7. Vue登录方式的切换

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

  8. 怎么解决JSP中出现乱码的问题

    首先我们先了解一下问题的原因.一般情况在在每个JSP页的头部都有这样一条语句: 这条语句决定了此页面使用GB2312编码形式,而在数据库中一般用的是iso-8859-1字符集存储数据. 而Java程序 ...

  9. ModelSim使用&dollar;display查看变量值和输出信息

    打开ModelSim,新建工程->新建Verilog文件demo.v 输入文件内容 module demo(); reg[3:0] a,b; initial begin $display(&qu ...

  10. c&plus;&plus; 数组元素拷贝到容器(copy)

    #include <iostream> // cout #include <algorithm> // copy #include <vector> // vect ...