杭电 5773 The All-purpose Zero

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可以变成任意数,求出他的最长上升子序列的长度,必须例一,将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<cstdio>
 2 #include<algorithm>
 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 }
时间: 2024-10-25 20:36:02

杭电 5773 The All-purpose Zero的相关文章

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

杭电1276--士兵队列训练问题

士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4605    Accepted Submission(s): 2148 Problem Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行

杭电1272 并查集找环+判断连通

杭电1272 并查集找环+判断连通 E - E Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B

杭电ACM Java实现样例

若使用Java求解杭电ACM,答案提交必须注意两点: 1.类名一定得是Main,否则服务器无法编译: 2.必须用while进行输入判断. 以1000题测试题为例,代码如下: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNextInt()) { int a=scan.n

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste