天天看點

A1045 Favorite Color Stripe (30 分)(最長不下降子序列問題)(DP)

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤10​4​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
           

Sample Output:

7
           

題意:

給出m種顔色作為主人公Eva喜歡的顔色(同時也是喜歡的順序),後給出一串長度為L的顔色序列。現在要去掉Eva不喜歡的顔色,然後求剩餘序列的一個子序列,使得這個子序列滿足喜歡的順序(不一定輸出所有顔色),且為滿足條件的子序列中長度最長的子序列。輸出其長度。

第一行:N 表示有多少種顔色

第二行:表示喜歡m = 5種顔色,分别為2 3 1 5 6

第三行:長度為L = 12 的序列

思路:

對于喜歡顔色的序列,可以做一個映射,映射到遞增序列,這樣問題就轉化為最長不下降子序列LIS的問題(要注意:選出的子序列不一定是連續的,例如{1,2,3,-1,-2,7,9},最長不下降子序列為{1,2,3,7,9})

注意:

  1. 顔色種類最多200,序列長度最多10000,要注意數組大小
  2. LIS的時間複雜度為O(L^2),這樣下來10 ^8 數量級比較大,但是LIS中數組連續操作,cache命中率較高,不會逾時
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxc = 210;       //最大顔色數
const int maxn = 10010;     //最大L
int HashTable[maxc];
int A[maxn], dp[maxn];
int main(){
    int n, m, x;
    scanf("%d%d", &n, &m);  //其實n不用
    memset(HashTable, -1, sizeof(HashTable));   //初始化為-1,表示不喜歡
    for(int i = 0; i < m; i++){
        scanf("%d", &x);
        HashTable[x] = i;       //映射
    }
    int L, num = 0;             //num存放喜歡顔色的總數
    scanf("%d", &L);
    for(int i = 0; i < L; i++){
        scanf("%d", &x);
        if(HashTable[x] >= 0){      //隻将喜歡顔色對應的映射數字存入數組a
            A[num++] = HashTable[x];
        }
    }
    //以下為LIS問題的模闆
    int ans = -1;       //記錄最大的dp[i]
    for(int i = 0; i < num; i++){   //按順序計算dp[i]的值
        dp[i] = 1;                  //邊界初始條件(即先假設每個元素自成一個序列)
        for(int j = 0; j < i; j++){
            if(A[j] <= A[i] && dp[i] < dp[j] + 1){
                dp[i] = dp[j] + 1;      //狀态轉移方程,用來更新dp[i]
            }
        }
        ans = max(ans, dp[i]);
    }
    printf("%d\n", ans);
    return 0;
}