http://acm.hdu.edu.cn/showproblem.php?pid=5773

The All-purpose Zero

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.

 
题意:在一个序列里面求最长严格上升子序列,其中0可以任意变换为其他数。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 100005 int dp[N];
int num[N];
/*
0可以转化成任意整数,包括负数,
显然求LIS时尽量把0都放进去必定是正确的。
因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,
统计结果的时候再算上0的数量。为了保证严格递增,
我们可以将每个权值S[i]减去i前面0的个数,再做LIS,
就能保证结果是严格递增的。
*/
int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
int n;
scanf("%d", &n);
int cnt = ;
int zero = ;
for(int i = ; i <= n; i++){
int a;
scanf("%d", &a);
if(a == ) zero++;
else {
num[++cnt] = a - zero;
}
}
if(cnt == ) {
printf("Case #%d: %d\n", cas, n);
continue;
}
memset(dp, , sizeof(dp));
int tot = ;
dp[] = num[];
for(int i = ; i <= cnt; i++) {
if(num[i] > dp[tot]) dp[++tot] = num[i];
else {
int pos = lower_bound(dp + , dp + tot, num[i]) - dp;
dp[pos] = num[i];
}
}
printf("Case #%d: %d\n", cas, tot + zero);
}
return ;
}

最新文章

  1. 剑指Offer 顺时针打印矩阵
  2. iOS UICollectionView之二(垂直滚动)
  3. The 50 Most Essential Pieces of Classical Music
  4. java基于xml配置的通用excel单表数据导入组件(五、Action处理类)
  5. C++引用(Reference)
  6. POJ3026 最小生成树
  7. JavaScript中Null和Undefined的深渊
  8. JUnit学习
  9. 命令导入导出oracle库
  10. Java Integer类型比较
  11. .NET-记一次架构优化实战与方案-前端优化
  12. Codeforces 714A 朋友聚会
  13. MATLAB 画柱状图(/直方图)修改横坐标名称并使其横着显示
  14. python 获取本机ip
  15. CentOS下安装Docker-CE
  16. read temperature
  17. RabbitMq Queue一些方法及参数
  18. HttpMessageNotWritableException: Could not write JSON: No serializer found for class ****
  19. python 字符串和整数,浮点型互相转换
  20. iOS开发-- 使用NSNumber将int、float、long等数据类型加入到数组或字典中

热门文章

  1. android反射组件 (一个)java 它们的定义annotation基础知识
  2. 自定义 DependencyProperty 与 RoutedEvent
  3. Layui 2.0.0 正式发布:潜心之作,开箱即用的前端UI框架(确实很多内容)
  4. StackLayout
  5. [PHP7.0-PHP7.2]的新特性和新变更
  6. C# 异步委托的使用
  7. centos搭建svn服务器并同步到web目录
  8. 图像滤镜艺术---球面(Spherize)滤镜
  9. Win10《芒果TV》更新v3.8.50勇敢版:新增短信和扫码登录
  10. Channel 9视频整理【1】