博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电 5773 The All-purpose Zero
阅读量:7238 次
发布时间:2019-06-29

本文共 2372 字,大约阅读时间需要 7 分钟。

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

272 0 2 1 2 0 561 2 3 3 0 0

Sample Output

Case #1: 5Case #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可以变成任意数,求出他的最长上升子序列的长度,必须例一,将2 0 5中的0变成3,最长子序列就是0 1 2 3 5。
 
对于这题不能直接求出最长上升子序列的长度,可以先让每个数减去它前面0的个数,再求出非0的数序列的最长上升子序列的长度,最后求出的长度加上零的数量。
 
假设让所有的零都进去最长连续子序列,例如a 0 0 0 b,可以先不看b,将a 0 0 0看成连续的上升序列,用b减去0的数量b-3,如果b-3>a说明a可以大于最后一个零的值,a b-3为非0数最长子序列,长度为2,加上0的个数,最后结果为5,如果b-3<a的值,说明最长子序列只能到最后一个零,a或b为非零数中最长子序列,长度为1,加上0的数量,最后结果为4,最后求的序列就是a 0 0 0(0为任意数)
 
1 #include
2 #include
3 #define INF 0x3f3f3f3f 4 using namespace std; 5 int b[100100],g[100100]; 6 int main() 7 { 8 int t,s=0; 9 scanf("%d",&t);10 while(t--)11 {12 int n,sum0=0;13 int i,a,j;14 scanf("%d",&n);15 int num=0;16 for(i = 1 ; i <= n ; i++)17 {18 scanf("%d",&a);19 g[i]=INF;20 if(a == 0)21 {22 sum0++;23 continue;24 }25 b[++num]=a-sum0; //记录每个数都减去前面的0的数量 26 }27 int max0=0;28 for(i = 1 ; i <= num ; i++)29 {30 int k=lower_bound(g+1,g+num+1,b[i])-g; //类似二分法,把b[i]的数存到g[i]中 31 max0=max0>k?max0:k; //直接记录最长子序列长度(相当于d[i]记录以第i个数结尾的子序列的最大长度,再比较d[i]的最大值) 32 g[k]=b[i];33 }34 printf("Case #%d: %d\n",++s,max0+sum0);35 }36 }

 

转载于:https://www.cnblogs.com/yexiaozi/p/5766240.html

你可能感兴趣的文章
js 对文件操作
查看>>
MySQL 5.6学习笔记(数据表基本操作)
查看>>
复制控制---复制构造函数
查看>>
bash把所有屏幕输出重定向到文件并保持屏幕输出的方法
查看>>
HBase 压缩算法设置及修改
查看>>
深入了解jquery中的键盘事件
查看>>
windows常用命令行整理
查看>>
DotNet中人民币符号的输出
查看>>
Spark源码分析 – SchedulerBackend
查看>>
正则表达式
查看>>
C语言中结构体 自引用 和 相互引用
查看>>
awk substr()函数
查看>>
git diff的用法
查看>>
[SQL基础]入门
查看>>
s3cmd 安装使用指南
查看>>
hdu 1253:胜利大逃亡(基础广搜BFS)
查看>>
sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)
查看>>
代码修改mysql字符
查看>>
Android 新加几个开源项目
查看>>
Mysql 不同版本 说明
查看>>