hdu 5773 最长递增子序列 (nlogn)+贪心
The All-purpose Zero
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 947 Accepted Submission(s): 453
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.
7
2 0 2 1 2 0 5
6
1 2 3 3 0 0
In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e5+10; int a[N],ans[N];
int main()
{
int cas,n,x,kk=0;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
int cnt=0,num=0;
for(int i=1;i<=n;i++){
scanf("%d",&x);
if(!x) cnt++;
else a[++num]=x-cnt;
}
if(!num) {
printf("Case #%d: %d\n",++kk,cnt);
continue;
}
int len=1;
ans[1]=a[1];
for(int i=2;i<=num;i++){
if(a[i]>ans[len]) ans[++len]=a[i];
else {
int pos=lower_bound(ans+1,ans+len,a[i])-ans;
ans[pos]=a[i];
}
}
printf("Case #%d: %d\n",++kk,len+cnt);
}
return 0;
}
0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的。因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,统计结果的时候再算上0的数量。为了保证严格递增,我们可以将每个权值S[i]减去i前面0的个数,再做LIS,就能保证结果是严格递增的。
hdu 5773 最长递增子序列 (nlogn)+贪心的更多相关文章
- HDU 5773 最长上升子序列
题意 给出一个序列 问它的最长严格上升子序列多长 这个序列中的0可以被替代为任何数 n的范围给出了1e5 所以平常的O(n*n)lis不能用了 在kuangbin的模板里有O(nlogn)的模板 套上 ...
- 最长递增子序列nlogn的做法
费了好大劲写完的 用线段树维护的 nlogn的做法再看了一下 大神们写的 nlogn 额差的好远我写的又多又慢 大神们写的又少又快时间 空间 代码量 哪个都赶不上大佬们的代码 //这是我写的 ...
- HDU-1160-FatMouse's Speed(DP, 最长递增子序列)
链接: https://vjudge.net/problem/HDU-1160 题意: FatMouse believes that the fatter a mouse is, the faster ...
- HDU 1257 最少拦截系统 最长递增子序列
HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...
- (转载)最长递增子序列 O(NlogN)算法
原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...
- 最长递增子序列 O(NlogN)算法
转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...
- 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现
关于最长递增子序列时间复杂度O(n^2)的实现方法在博客http://blog.csdn.net/iniegang/article/details/47379873(最长递增子序列 Java实现)中已 ...
- 【LeetCode】300.最长递增子序列——暴力递归(O(n^3)),动态规划(O(n^2)),动态规划+二分法(O(nlogn))
算法新手,刷力扣遇到这题,搞了半天终于搞懂了,来这记录一下,欢迎大家交流指点. 题目描述: 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列是由数组派生而来的序列,删除(或不删 ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...
随机推荐
- 同一台服务器请求easyswoole的一个websocket接口报错
求助大神啊!file_get_contents报这个错:failed to open stream: Connection timed out换成curl又报这个错:couldn't connect ...
- python3列表、元组
列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作.列表中的每个元素都分配一个数字也就是它的位置,或叫索引,第一个索引是0,第二个索引是1,依此类推. ...
- 搞懂Dubbo SPI可拓展机制
前言 阅读本文需要具备java spi的基础,本文不讲java spi,please google it. 一.Dubbo SPI 简介 SPI(Service Provider Interface) ...
- C#中属性的封装
封装的一般性 封装快捷键:Ctrl+R+E 封装的目的: public修饰的属性,不够安全:private修饰的属性,无法使用所以,用到了封装: 封装就是 隐藏对象的信息,但要流出访问的接口 封装代码 ...
- EasyUI_前台js_省市县三级联动
1.html: <td class="tdl">所属城市</td> <td class="td_detail"> <i ...
- squoosh
谷歌在线压缩图片
- nop4.1学习ServiceCollectionExtensions(二)(ioc,ef,ef连接的实现)
这个是获取程序路径,并初始化文件管理类 初始化插件管理 接下来就是注册服务和autoafc 在INopStartup配置sql连接,插件,mvc等 配置了sql连接 数据库的配置类 在AddAutoM ...
- GNU g++常用编译选项用法
GNU g++常用编译选项用法 本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/30686/showart_1210761.html GCC ...
- 如何为 esp32 编译和配置及烧写 MicroPython 固件。
MicroPython 在 esp-idf (esp32) 上编译固件 esp32 编译 micropython 的固件相关的资料应该很多吧,我也会出一篇,但会额外讲一些 linux 的东西的. 资料 ...
- python获取文件及文件夹大小
Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsi ...