gym 101194 C Mr. Panda and Strips

題意

找到一個連續區間和另一個連續區間,它兩拼起來也要求每個數都互不相同,問拼起來後最大的長度是多長 (n<=1000)

題解

首先預處理出每位數字i左邊第一個出現的位置和右邊第一個出現的位置(用來快速確定互不相同的連續區間)之後枚舉左區間的右端點,向左擴充連續區間將其中的位置丟入map,之後枚舉右區間的左端點,向右擴充,邊擴充邊統計答案(即邊擴充邊縮放答案)

代碼

/**
 *     author:     TelmaZzzz
 *     create:     2019-10-04-20.33.20
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
#define Fi first
#define Se second
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
//int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
void TelmaZzzz(){
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
}
 
int a[N];
int Left[N],Right[N];
int pos[N];
map<int,int>mp;
int main(){
    TelmaZzzz();
    //ios::sync_with_stdio(false);
    int t;
    R(t);
    int n;
    int ti=0;
    while(t--){
        R(n);
        rep(i,1,n){
            R(a[i]);
        }
        rep(i,1,n) pos[i]=0;
        rep(i,1,n){
            Left[i]=pos[a[i]];
            pos[a[i]]=i;
        }
        rep(i,1,n) pos[i]=n+1;
        erp(i,n,1){
            Right[i]=pos[a[i]];
            pos[a[i]]=i;
        }
//        rep(i,1,n) cout<<Right[i]<<' ';
//        cout<<endl;
        int ans=0;
        rep(i,1,n){
            int l=Left[i];
            int now=i;
            mp.clear();
            while(now>l){
                mp[a[now]]=now;
                l=max(l,Left[now]);
                now--;
            }
            rep(j,i+1,n){
                int arr=mp.size();
                int ARR=mp.size();
                int r=Right[j];
                now=j;
                while(now<r){
                    //cout<<now<<' '<<j<<' '<<i<<' '<<r<<' '<<arr<<' '<<ARR<<endl;
                    if(mp.find(a[now])==mp.end()){
                        r=min(r,Right[now]);
                        ans=max(ARR+now-j+1,ans);
                        now++;
                    }
                    else {
                        ARR=min(ARR,i-mp[a[now]]);
                        ans=max(ARR+now-j+1,ans);
                        r=min(r,Right[now]);
                        now++;
                    }
                }
                //cout<<i<<' '<<j<<' '<<ans<<endl;
            }
        }
        printf("Case #%d: ",++ti);
        W(ans);
    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章