zoukankan      html  css  js  c++  java
  • 杭电 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
    ",++s,max0+sum0);
    35     }
    36 }
  • 相关阅读:
    把IDEA中新建的项目提交到Github仓库中
    在IDEA中设置方法自动注释(带参数和返回值)
    如何在 Maven 工程中引入其他jar包 并生效?(以 Netty 为例)
    在 IDEA 中 配置 Maven
    Visio中锁定元件
    DevExpress中 TreeList控件的常规配置
    从SuperSocket的App.config中读取配置,并修改保存,再重启服务
    devexpress 中 XtraTabcontrol 改变BackColor 的方法
    DevExpress 中 GridControl 的数据源DataTable 内容改变后 重新绑定
    如何在linux中设置tab键长度
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5766240.html
Copyright © 2011-2022 走看看