[2016-2017 ACM-ICPC CHINA-Final]Problem C. Mr. Panda and Strips(DP+线段树)

[2016-2017 ACM-ICPC CHINA-Final]Problem C. Mr. Panda and Strips(DP+线段树)

题目链接:

(https://codeforces.com/gym/101194)

题面:

题意:

给定一个长度为\(\mathit n\)的数列,你可以选择两个不互相交的连续子序列或者一个连续子序列。要求选择的子序列中不能有重复的数字,问你最多能包含多少种数字(即长度最大)?

思路:

首先\(O(n^2logn)/O(n^2+nlogn)\)复杂度内动态规划预处理出\(dp[i][j]\)代表\([i,j]\)这个区间内可以最长的不包含重复数字的连续子序列。

我们设答案由两个区间构成,其中右区间可以为空区间。

然后我们枚举左区间的左端点\(i\in[1,n]\),然后枚举左区间的右端点\(j\in[i,n]\)

对于每一个右端点\(\mathit j\),我们可以用\(set\)判断\([i,j-1]\)中是否已经出现过\(c_j\)

若已经出现过,则\(j-1\),已经是\(\mathit i\) 作为左端点的极大右端点了。右移左端点即可。

若未出现过,则代表\([i,j]\)是一个合法的区间,我们用线段树维护\([j+1,n]\)中最长的合法区间。

方法:我们维护区间中每一个位置\(k\)当且是被哪个最长的合法区间包含。需要两颗线段树,一个维护区间的左端点,另一位维护区间的右端点。配合一个multiset存区间的dp值。

对于每一个\(\mathit j\),我们将在右边区间中的所有\(c_j\)值得位置\(pos\)更新,若\(pos\)当且在区间\([l,r]\),因为已经不能再取\(c_j\)这个数字了,所以我们将\(dp[l][r]\)拆为\(dp[l][pos-1],dp[pos+1][r]\)两个区间。对应得就是线段树得单点查询,区间更新和multiset得删除和插入操作。

对每一个右端点\(\mathit j\),然后更新答案\(j-i+1+multiset.maxnum\)

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int a[maxn];
struct node {
    int l, r;
    int val;
} segment_tree[maxn << 2][2];
void build(int rt, int l, int r, int v, int id)
{
    segment_tree[rt][id].l = l;
    segment_tree[rt][id].r = r;
    segment_tree[rt][id].val=0;
    if (l == r) {
        segment_tree[rt][id].val = v;
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid, v, id);
    build(rt << 1 | 1, mid + 1, r, v, id);
}
void pushdown(int rt, int id)
{
    if (segment_tree[rt][id].val) {
        segment_tree[rt << 1][id].val = segment_tree[rt][id].val;
        segment_tree[rt << 1 | 1][id].val = segment_tree[rt][id].val;
        segment_tree[rt][id].val = 0;
    }
}
void update(int rt, int l, int r, int v, int id)
{
    if(l>r)
        return ;
    if (segment_tree[rt][id].l >= l && segment_tree[rt][id].r <= r) {
        segment_tree[rt][id].val = v;
        return ;
    }
    pushdown(rt, id);
    int mid = (segment_tree[rt][id].l + segment_tree[rt][id].r) >> 1;
    if (mid >= l) {
        update(rt << 1, l, r, v, id);
    }
    if (mid < r) {
        update(rt << 1 | 1, l, r, v, id);
    }
}
int query(int rt, int pos, int id)
{
    if (segment_tree[rt][id].l == pos && segment_tree[rt][id].r == pos) {
        return segment_tree[rt][id].val;
    }
    pushdown(rt, id);
    int mid = (segment_tree[rt][id].l + segment_tree[rt][id].r) >> 1;
    if (mid >= pos) {
        return query(rt << 1, pos, id);
    } else {
        return query(rt << 1 | 1, pos, id);
    }
}
std::vector<int> v[100010];
int dp[1002][1002];
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    for (int icase = 1; icase <= t; ++icase) {
        n = readint();
        repd(i, 1, n) {
            a[i] = readint();
            v[a[i]].pb(i);
        }
        int ans = 1;
        set<int> vis;
        multiset<int> st;
        repd(i, 1, n) {
            int j;
            for (j = i; j <= n; ++j) {
                if (vis.count(a[j]) == 0) {
                    dp[i][j] = j - i + 1;
                    vis.insert(a[j]);
                } else {
                    break;
                }
            }
            for (; j <= n; ++j) {
                dp[i][j] = dp[i][j - 1];
            }
            vis.clear();
        }
        repd(len, 2, n - 1) {
            repd(i, 1, n) {
                int j = i + len - 1;
                if(j<=n)
                    dp[i][j] = max(dp[i][j], max(dp[i + 1][j], dp[i][j - 1]));
            }
        }
        repd(i, 1, n) {
            build(1, i, n, i, 0);
            build(1, i, n, n, 1);
            st.insert(dp[i][n]);
            st.insert(0);
            repd(j, i, n) {
                if (vis.count(a[j]) == 0) {
                    vis.insert(a[j]);
                    for (auto pos : v[a[j]]) {
                        if (pos >= i) {
                            int l = query(1, pos, 0);
                            int r = query(1, pos, 1);
                            update(1, pos + 1, r, pos + 1, 0);
                            update(1, l, pos - 1, pos - 1, 1);
                            auto it = st.lower_bound(dp[l][r]);
                            st.erase(it);
                            if (l < r) {
                                st.insert(dp[l][pos - 1]);
                                st.insert(dp[pos + 1][r]);
                            }
                        }
                    }
                    ans = max(ans, (*(--st.end())) + j - i + 1);
                } else {
                    break;
                }
            }
            vis.clear();
            st.clear();
        }
        printf("Case #%d: %d\n", icase, ans );
        repd(i, 1, n) {
            v[a[i]].clear();
        }
        repd(i, 1, n) {
            repd(j, 1, n) {
                dp[i][j] = 0;
            }
        }
    }

    return 0;
}



posted @ 2020-12-03 21:23  茄子Min  阅读(155)  评论(0编辑  收藏  举报