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

Problem Description
?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.
 
Input
The first line contains an interger T,denoting the number of the test cases.(T <= 10)
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.
 
Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.
 
Sample Input
2
7
2 0 2 1 2 0 5
6
1 2 3 3 0 0
 
Sample Output
Case #1: 5 Case #2: 5

Hint

In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.

题意: 给你n个数字,你可以将其中的0变成任意数字,求最终能得到的最长严格递增子序列,
#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)+贪心的更多相关文章

  1. HDU 5773 最长上升子序列

    题意 给出一个序列 问它的最长严格上升子序列多长 这个序列中的0可以被替代为任何数 n的范围给出了1e5 所以平常的O(n*n)lis不能用了 在kuangbin的模板里有O(nlogn)的模板 套上 ...

  2. 最长递增子序列nlogn的做法

    费了好大劲写完的  用线段树维护的 nlogn的做法再看了一下 大神们写的 nlogn  额差的好远我写的又多又慢  大神们写的又少又快时间  空间  代码量 哪个都赶不上大佬们的代码 //这是我写的 ...

  3. HDU-1160-FatMouse's Speed(DP, 最长递增子序列)

    链接: https://vjudge.net/problem/HDU-1160 题意: FatMouse believes that the fatter a mouse is, the faster ...

  4. HDU 1257 最少拦截系统 最长递增子序列

    HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...

  5. (转载)最长递增子序列 O(NlogN)算法

    原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...

  6. 最长递增子序列 O(NlogN)算法

    转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...

  7. 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现

    关于最长递增子序列时间复杂度O(n^2)的实现方法在博客http://blog.csdn.net/iniegang/article/details/47379873(最长递增子序列 Java实现)中已 ...

  8. 【LeetCode】300.最长递增子序列——暴力递归(O(n^3)),动态规划(O(n^2)),动态规划+二分法(O(nlogn))

    算法新手,刷力扣遇到这题,搞了半天终于搞懂了,来这记录一下,欢迎大家交流指点. 题目描述: 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列是由数组派生而来的序列,删除(或不删 ...

  9. 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN

    题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...

随机推荐

  1. Linux7_MySQL5.7_主从复制_scripts

    # cat my_full_backup.sh #!/bin/bash BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"` format_time=`dat ...

  2. C# 定义热键

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 409 Conflict - PUT https://registry.npm.taobao.org/-/user/org.couchdb.user:zphtown - [conflict] User xxx already exists

    解决方法cmd执行 npm config set registry https://registry.npmjs.org/ 为什么,参考此文档:https://blog.csdn.net/adc_go ...

  4. vue入门:(class与style绑定)

    对象语法 数组语法 一.对象语法 1.1对象语法绑定HTML Class 语法:v-bind:class="{'className1':boolean1,'className2':boole ...

  5. 【异常】Caused by: java.lang.IllegalStateException: Zip64 archives are not supported

    1 自己打包Spring boot项目依赖了第三方的Phoenix jar包过大,导致启动后报错 参考了这篇博客:https://cloud.tencent.com/developer/ask/135 ...

  6. Linux系统安装常用开发软件

    vim.jdk.tomcat.mysql 安装vim(命令模式=>编辑模式=>底行模式) [root@localhost ~]# yum install vim*结束后一直确认即可,键入y ...

  7. 1.RPC原理学习

    1.什么是RPC:远程过程调用协议 RPC(Remote Procedure Call Protocol)— 远程过程调用协议,是一种通过网络从远程计算机程序上请求服务,而不需要 了解底层网络技术的协 ...

  8. LNMP安装与配置之CentOS7的安装。

    LNMP与LAMP是我们常见的两种网站服务器架构.LNMP代表的就是Linux系统下Nginx+MySQL+PHP/Python,LAMP代表的则是Linux系统下Apache+MySQL+PHP/P ...

  9. .NET 树型递归

    /// <summary> /// 获取全部水价标准模型 /// </summary> /// <returns></returns> public I ...

  10. 22_7mybaits注解开发

    这几年来注解开发越来越流行,Mybatis 也可以使用注解开发方式,这样我们就可以减少编写 Mapper 映射文件了. 1.常用注解说明 @Insert:实现新增 @Update:实现更新 @Dele ...