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 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
随机推荐
- vs code 快速生成vue 模板
vs code 快速生成vue 模板 1.使用快捷Ctrl + Shift + P唤出控制台,然后输入snippets并选择.(或 文件>首选项>用户代码片断里面,输入 vue.json ...
- 转贴:获取元素CSS值之getComputedStyle方法熟悉
获取元素CSS值之getComputedStyle方法熟悉 一.碎碎念~前言 我们都用过jQuery的CSS()方法,其底层运作就应用了getComputedStyle以及getPropertyVal ...
- ruby on rails, api only, 脚手架
rails new connector_api --api --database=postgresql bundle install rake db:create rails g scaffold i ...
- ubuntu/linuxmint如何添加和删除PPA源
[添加] 1.sudo add-apt-repository ppa:user/ppa-name 2.sudo apt-get update (然后再安装软件sudo apt-get install ...
- P4971 断罪者
传送门 首先,不难看出可以给每个集合开一个可并堆,然后乱搞就可以了 主要的问题就是将罪恶值清零和减少罪恶值该怎么搞 罪恶值清零可以直接找到这个节点然后把值变为零,再把它的左右儿子分别并到这个节点所在的 ...
- MySql 同表复制数据 可以改变数据
Mysql语法: INSERT INTO 表名 (字段) SELECT 字段 FROM 表名 WHERE 条件: 如果要修改其中某一个字段,在查询语句中:x(要改变的值) as 字段名. eg: IN ...
- Spring: (一) -- 春雨润物之 核心IOC
作为一个Java人,想必都或多或少的了解过Spring.对于其优势也能道个一二,诸如方便解耦.支持AOP编程.支持声明式事务.方便测试等等.Spring也不仅仅局限于服务器端开发,它可以做非常多的事情 ...
- (8)string对象上的操作1
读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...
- python计算auc指标
1.安装scikit-learn 1.1Scikit-learn 依赖 Python (>= 2.7 or >= 3.3), NumPy (>= 1.8.2), SciPy (> ...
- Spartan6系列之芯片配置模式详解
1. 配置概述 Spartan6系列FPGA通过把应用程序数据导入芯片内部存储器完成芯片的配置.Spart-6 FPGA可以自己从外部非易失性存储器导入编程数据,或者通过外界的微处理器.DSP等对 ...