LA 3029 Subsequence
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的更多相关文章
- LA 3029 City Game
LA 3029 求最大子矩阵问题,主要考虑枚举方法,直接枚举肯定是不行的,因为一个大矩阵的子矩阵个数是指数级的,因此应该考虑先进行枚举前的扫描工作. 使用left,right,up数组分别记录从i,j ...
- LA 2678 Subsequence(二分查找)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- LA 2678 Subsequence
有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S ...
- LA 3029 - City Game (简单扫描线)
题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 ...
- UVa LA 3029 City Game 状态拆分,最大子矩阵O(n2) 难度:2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- LA 2678 – Subsequence
看到限时3S,自己写了一个二重循环的,然后华丽的 TLE...T T 瞄了瞄书上,作者的思路果然是很好.膜拜中. 他只枚举了终点,然后用二分查找. 用到了lower_bound函数,这个lower_b ...
- 【巧妙预处理系列】【UVA1330】City game
最大子矩阵(City Game, SEERC 2004, LA 3029) 给定一个m×n的矩阵,其中一些格子是空地(F),其他是障碍(R).找出一个全部由F组成的面积最大的子矩阵,输出其面积乘以3后 ...
- HDU 1159:Common Subsequence(LCS模板)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
随机推荐
- bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
- bzoj2338
计算几何 我们先把所有的线段求出来,我们发现只有两个线段等长且中点重合时才能构成矩形,那么线段有n*n条,我们按中点,长度排序,然后对于一条线段扫描所有符合条件的线段计算答案,这样看起来是O(n^3) ...
- mac+php+xdebug
1,下载xdebug 2,进入xdebug-2.4.0RC4目录,运行phpize命令, 2,google之后说要安装autoconf brew install autoconf 3,但是使用brew ...
- bzoj 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪【枚举】
4维枚举平方小于10000的数,相加等于n则ans++ #include<iostream> #include<cstdio> using namespace std; con ...
- extjs grid禁止表格头部使用鼠标拖拽改变宽度
extjs6 经典版 表格头部使用鼠标拖动 禁止改变列的宽度 只需要给grid 设置属性enableColumnResize:false就可以啦 xtype:'grid', enableColumnR ...
- java虚拟机全集(31篇文章)
深入理解java虚拟机系列 深入理解Java虚拟机笔记---内存区域 深入理解Java虚拟机笔记---判断对象是否存活 深入理解Java虚拟机笔记---垃圾收集算法 深入理解Java虚拟机笔记---垃 ...
- spring cloud config搭建说明例子(三)-添加actuator
添加心跳 服务端 ConfigServer pom.xml添加actuator包 <dependency> <groupId>org.springframework.cloud ...
- [转]Linux系统调用--fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control头文件: #include <unistd.h> #include & ...
- 利用 nodeJS 搭建一个简单的Web服务器(转)
下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...
- fiddler不同代理模式的区别
Fiddler有不同的代理模式,分为以下两种: 流模式(Streaming)和缓冲模式(Buffering). 流模式可以理解为一种实时通信的模式,有请求就有返回,也就是实时返回. 缓冲模式是等所有请 ...