The All-purpose Zero

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 584    Accepted Submission(s): 278

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.

 
Author
FZU
思路:0可以变为任何的数,考虑到底以一个数的结尾的最长上升序列,加入前面有0的个数多少来变化能使这个长度变为最长,因为要加入0来变化,所以一定可以改变0的大小来使这个序列递增,但是插入0是有花费的,比如4 0 5;那么这个时候只能让当前的数减去前面0的个数,来抵消这个花费,因为每个0只消耗一点花费,所以将前面所有0加入,可保证到这个
数的序列最长。然后先将非0的求LIS,末尾加个最大值,然后加上前面的0,求最大;
 1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 using namespace std;
11 typedef long long LL;
12 int arr[100005];
13 int id[100005];
14 int a[100005];
15 int add[100005];
16 int c[100005];
17 int main(void)
18 {
19 int i,j,k;
20 scanf("%d",&k);
21 int __ca=0;
22 while(k--)
23 {
24 __ca++;
25 int cnt;
26 memset(c,0,sizeof(c));
27 scanf("%d",&cnt);
28 for(i=1; i<=cnt; i++)
29 {
30 scanf("%d",&a[i]);
31 }
32 a[cnt+1]=1e9;
33 cnt++;
34 for(i=1; i<=cnt; i++)
35 {
36 if(a[i]==0)
37 {
38 c[i]=c[i-1]+1;
39 }
40 else
41 {
42 c[i]=c[i-1];
43 }
44 }
45 int ans=1;
46 for(i=1; i<=cnt; i++)
47 {
48 if(a[i]!=0)
49 {
50 arr[ans]=a[i]-c[i];
51 add[ans]=c[i];
52 ans++;
53 }
54 }
55 id[1]=arr[1];
56 int maxx=1;
57 int ac=0; ac=max(ac,add[1]+maxx);
58 for(i=2; i<ans; i++)
59 {
60 if(arr[i]>id[maxx])
61 {
62 maxx++;
63 id[maxx]=arr[i];
64 ac=max(ac,add[i]+maxx);
65 }
66 else
67 {
68 if(arr[i]==id[maxx])
69 continue;
70 else
71 {
72 int ask=0;
73 int l=1;
74 int r=maxx;
75 while(l<=r)
76 {
77 int mid=(l+r)/2;
78 if(id[mid]<=arr[i])
79 {
80 ask=mid;
81 l=mid+1;
82 }
83 else r=mid-1;
84 }
85 ac=max(ac,add[i]+ask+1);
86 maxx=max(maxx,ask+1);
87 id[ask+1]=min(id[ask+1],arr[i]);
88 }
89 }
90 }
91 printf("Case #%d: %d\n",__ca,ac-1);
92 }
93 return 0;
94 }

最新文章

  1. SQL Server 隐式转换引发的躺枪死锁-程序员需知
  2. oracle分组查询实例ORA-00979和ORA-00937错误分析
  3. Robberies(HDU2955):01背包+概率转换问题(思维转换)
  4. php 方便快捷导出excel
  5. p ython笔记第三天
  6. 在百万数据中找出重复的数据sql
  7. dubbo监控活跃线程数
  8. Oracle运维 专业的事情交给专业的人来做
  9. 快捷查看dll的PublicKeyToken
  10. 03-IOSCore - XML及解析、Plist
  11. 制作win10 usb 启动盘
  12. js中bind、call、apply函数的用法 (转载)
  13. Linux知识积累(8)卸载安装jdk
  14. ios12版本以上键盘唤起后,收回页面不回滚问题
  15. Sessions Hang on row cache lock
  16. Linux里的eval命令
  17. 简单的三级联动demo
  18. 语音VLAN异常流量分析
  19. 21 go并发编程-下
  20. InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClas...

热门文章

  1. C#表头固定
  2. MapReduce07 Join多种应用
  3. Leetcode中的SQL题目练习(一)
  4. Java8使用并行流(ParallelStream)注意事项
  5. 视频框架 Vitamio使用
  6. oracle(数据文件)
  7. 【Linux】【Services】【SaaS】Docker+kubernetes(6. 安装和配置ceph)
  8. 【Java 多线程】Java线程池类ThreadPoolExecutor、ScheduledThreadPoolExecutor及Executors工厂类
  9. ES在项目中的测试
  10. AOP中环绕通知的书写和配置