1. 程式人生 > >DP-HDU-5773-The All-purpose Zero

DP-HDU-5773-The All-purpose Zero

The All-purpose Zero

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

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

Source
2016 Multi-University Training Contest 4

題意:
給你一個數組,其中的0可以變成任何數,求最長的LIS是多長。

題解:
因為0可以變成任何數,那麼所有的0是絕對要用上的(就算強行用上,頂多是使原LIS保持長度,不會出現用0減長度的情況)。所以把所有的0單獨拿出來後,看剩下數的LIS,為了求出的LIS保證在原位置加入0後仍然是LIS,我們將原本非0的元素都減去前方0的個數再去求LIS,這樣就能保證如果此數在LIS中,那麼加入0以後也一定是LIS。
因為資料大小和時限的原因,用一個二分nlogn的LIS即可。

//
//  main.cpp
//  160728-4
//
//  Created by 袁子涵 on 16/7/28.
//  Copyright © 2016年 袁子涵. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;
const int MAXN=100005;
int t,cas;
long long int n,b[MAXN],out,tmp;
long long int num[MAXN];
long long int Search(long long int num,long long int low,long long int high)
{
    long long int mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(num>=b[mid])  low=mid+1;
        else   high=mid-1;
    }
    return low;
}
long long int DP(long long int n)
{
    long long int i,len,pos;
    b[1]=num[1];
    len=1;
    for(i=2;i<=n;i++)
    {
        if(num[i]>b[len])
        {
            len=len+1;
            b[len]=num[i];
        }
        else
        {
            pos=Search(num[i],1,len);
            b[pos]=num[i];
        }
    }
    return len;
}
long long int total=0;
bool flag;
int main(int argc, const char * argv[]) {
    scanf("%d",&t);
    while (t--) {
        long long int now=0;
        total=0;
        cas++;
        scanf("%lld",&n);
        for (long long int i=1; i<=n; i++) {
            scanf("%lld",&tmp);
            if (tmp==0)
                total++;
            else
                num[++now]=tmp-total;
        }
        out=DP(now);
        if (total==n)
            out=0;
        cout << "Case #" << cas << ": " << out+total << endl;
    }
    return 0;
}

相關推薦

DP-HDU-5773-The All-purpose Zero

The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 279

HDU 5773 The All-purpose Zero(LIS特殊處理)

Problem Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1

5773 The All-purpose Zero LIS變形 思維+dp

題目連結 題意: 可以將0替換成任意interger(包括負數),在此基礎上求最長遞增子序列。 思路: 對於這個題目,我只想%一下出題大佬! 首先要知道,把所有的0都用上一定不會使答案變得更

The All-purpose Zero(喜歡這道題)

The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 553    Accep

hdu5773The All-purpose Zero

題意:給一個n個元素的陣列,其中0可以變成任意整數,求最長上升子序列。 分析:比賽的時候一直想dp狀態,卻一直都想不到,其實和另外一題挺像的:要求改變最少個數,使得整個陣列嚴格遞增。其實就是將自身和

hdu 4355 Party All the Time(三分)

while pid const d+ ans acm space += bits 題目鏈接:hdu 4355 Party All the Time 題意: 有n個人,在一個一維的坐標軸上,現在讓他們聚在一起。 每個人移動一段距離會產生一個 不開心值=S3*W,現在問你最小的

hdu 4901 The Romantic Hero 計數dp,位計算

height accep fin -a can stop ott from include The Romantic Hero Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072

HDU 5542 The Battle of Chibi dp+樹狀數組

mod sizeof tac pro sum cpp case name problem 題目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 題意:給你n個數,求其中上升子序列長度為m的個數 可以考慮用dp[i][j]表示

HDU 5956 The Elder (樹上斜率DP)

continue 進行 clas pri 給定 題意 sig 復雜度 long 題意:給定上一棵樹,然後每條邊有一個權值,然後每個點到 1 的距離有兩種,第一種是直接回到1,花費是 dist(1, i)^2,還有另一種是先到另一個點 j,然後兩從 j 向1走,當然 j 也可

HDU 4028 The time of a day (dp+離散化)

題意:給你1~n的數字,問你一個集合中的lcm大於m的集合有多少個 思路:這個題挺有意思的,我們直接的可以想到爆枚的話的複雜度有2^40,但是這些數中的lcm的答案缺不會有很多,最多也就是這40個數的lcm,所以不會有很大,那這樣的話我們用一個map來記錄dp[i]代表當前是有前i個數,對於每i個數的map

hdu-4489-The King’s Ups and Downs(DP

                                                                         Problem Description The king has guards of all different heights

HDU 5501 The Highest Mark (貪心+揹包DP)*

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

HDU 1561 The more, The Better(樹形DP+01揹包)

題目連結 題意(這好像是中文題,不會別的語言=_=||) 思路 我們以0為根節點向外建樹,以前置點為起點,該點為終點,該點價值為線的權值。 由於每個結點只有一個父節點,每點權值等價兩點之間線上的權值。

HDU 5956 The Elder(樹型DP+斜率優化)

題目連結 Problem Description Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom co

hdu-1561 The more, The Better (樹形dp入門,有依賴的揹包問題

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4954    

HDU 1561 The more, The Better(多重揹包+樹形DP)

Problem Description ACboy很喜歡玩一種戰略遊戲,在一個地圖上,有N座城堡,每座城堡都有一定的寶物,在每次遊戲中ACboy允許攻克M個城堡並獲得裡面的寶物。但由於地理位置原因,有些城堡不能直接攻克,要攻克這些城堡必須先攻克其他某一個特定的城堡。你能幫A

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

給他 cas 維數 color cost 代碼 01背包 size code HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 題意:

hdu 5381 The sum of gcd 2015多校聯合訓練賽#8莫隊算法

names 來看 efi nbsp span ems multipl script there The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K

hdu 1679 The Unique MST (克魯斯卡爾)

plm () pst cati 卡爾 nts 相等 cat get The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24152 Accepted:

hdu 4717 The Moving Points(三分)

fine names -- sqrt blank .cn class 題目 col 題目鏈接:hdu 4717 The Moving Points 題意: 在二維平面上有n個點,每個點給出移動的方向和速度。 問在某個時刻,這些點中最大距離最小是多少,輸出時刻和距離。 題解: