Codeforces Round #650 (Div. 3) ~~ F2. Flying Sort (Hard Version) (dp)

This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on n are greater than in the easy version of the problem.
You are given an array a of n integers (the given array can contain equal elements). You can perform the following operations on array elements:
choose any index i (1≤i≤n) and move the element a[i] to the begin of the array;
choose any index i (1≤i≤n) and move the element a[i] to the end of the array.
For example, if n=5, a=[4,7,2,2,9], then the following sequence of operations can be performed:
after performing the operation of the first type to the second element, the array a will become [7,4,2,2,9];
after performing the operation of the second type to the second element, the array a will become [7,2,2,9,4]
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the a array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities a[1]≤a[2]≤…≤a[n].

Output

For each test case output one integer — the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.

Example
Input

9
5
4 7 2 2 9
5
3 5 8 1 7
5
1 2 2 4 5
2
0 1
3
0 1 0
4
0 1 0 0
4
0 1 0 1
4
0 1 0 2
20
16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9

Output

2
2
0
0
1
1
1
1
16

Note

In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: [4,7,2,2,9]→[2,4,7,2,9]→[2,2,4,7,9].
In the second test case, you need to move the 1 to the beginning of the array, and the 8 — to the end. Therefore, the desired sequence of operations: [3,5,8,1,7]→[1,3,5,8,7]→[1,3,5,7,8].
In the third test case, the array is already sorted.

思路:找到最长的相邻子序列,比如:0 1 0 2,最长的相邻子序列为(0 1 2),另外最左边和最右边相同元素不必取完,但中间的值相等的元素必须取完。所以用dp可以做。

#include
const int maxn = 1e6+1;
using namespace std;
    
int dp[maxn][2]; //dp[0] 维护左端点, dp[1] 维护右端点

struct node {
    int ID;    // 原始位置
    int rank;  // 离散化的相对位置
    int val;   // 元素的值
    int pre;   // 离散化的前一个元素的值
    int e;     // 值相同的元素中最后一个元素的原始位置
}a[maxn];

bool op1(node n1, node n2) { // 按元素的值排序(离散化)
    if (n1.val == n2.val) return n1.ID < n2.ID;
    return n1.val < n2.val;
}

bool op2(node n1, node n2) {  // 恢复原排序
    return n1.ID < n2.ID;
}
map<int,int> d;
int main() {
    int T; scanf("%d", &T);
    while(T --) {
        int n; scanf("%d", &n);
        for(int i = 1; i <= n; ++ i) {
            scanf("%d", &a[i].val); a[i].ID = i;
            dp[i][0] = dp[i][1] = d[a[i].val] = 0;
        }
        sort(a+1, a+1+n, op1); // 离散化
        int tot = 0;
        for(int i = 1; i <= n; ++ i) {
            a[i].rank = ++tot;
            a[i].e = -1;
            if (i == 1) a[i].pre = -1;
            else if (a[i].val == a[i-1].val) a[i].pre = a[i-1].pre;
            else a[i].pre = a[i-1].val, a[i-1].e = a[i-1].ID;
        }         
        sort(a+1, a+1+n, op2); // 恢复排序
        int res = 0;
        for(int i = 1; i <= n; ++i) {
            d[a[i].val] ++;
            if (dp[a[i].rank-1][0] == 0 && d[a[i].val] == 1) dp[a[i].rank][0] = d[a[i].pre] + 1; // 存在逆序,前值是端点。
            else dp[a[i].rank][0] = dp[a[i].rank-1][0] + 1;
            if (d[a[i].val] == 1) dp[a[i].rank][1] = d[a[i].pre]+1; // 该值第一个元素
            else dp[a[i].rank][1] = max(d[a[i].pre]+1, dp[a[i].rank-1][1]+1); 

            if (a[i].e == i) d[a[i].val] = dp[a[i].rank][0]; // dp[0] 维护 d[val] 
            res = max(res, dp[a[i].rank][1]); 
        }
        printf("%d\n", n-res);
        d.clear();
    }
    return 0;
}

你可能感兴趣的:(dp)