題意:給出一個長度爲n的數列 和一個c 數列的全部數都在c範圍內node
問: 求一個最長子串 知足下列條件:c++
1 全部奇數位置的數相等ide
2 全部偶數位置的數相等spa
3 偶數位置和奇數位置的數不相等code
比賽的時候枚舉c 而後二分查找n o( c^2 logn ) 780msblog
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define inf 0x3f3f3f3f #define lson l,m,pos<<1 #define rson m+1,r,pos<<1|1 #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// const int N=1e4+5; const int mod=1e9+7; vector<int>a[105]; vector<int>::iterator it; int n,m,c,x; int check(int i,int j) { if( !a[i].size()||!a[j].size() )return 0; int ans=1; int pos=a[i][0]; while(1) { it=lower_bound(a[j].begin(),a[j].end(),pos ); if(it==a[j].end())return ans; pos=*it;ans++; it=lower_bound(a[i].begin(),a[i].end(),pos ); if(it==a[i].end())return ans; pos=*it;ans++; } } int main() { while(~RII(n,c)) { rep(i,1,n) { RI(x);a[x].pb(i); } int ans=0; rep(i,1,c) rep(j,1,c) if(i!=j)ans=max(ans,check(i,j)); printf("%d\n",ans); rep(i,1,100)a[i].clear(); } return 0; }
能夠dp作 o(nc) 100msinput
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s) #define ll long long #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) typedef pair<int,int>pii; ////////////////////////////////// const int N=200; struct node{int ans,now; }dp[N][N]; int n,m,a[100005],c; int sol() { rep(i,1,c)rep(j,1,c)dp[i][j].ans=0,dp[i][j].now=i; rep(i,1,n)RI(a[i]); rep(i,1,n) { int x=a[i]; rep(j,1,c) { if(x==j)continue; if(dp[x][j].now==x){dp[x][j].ans++;dp[x][j].now=j; } if(dp[j][x].now==x){dp[j][x].ans++;dp[j][x].now=j; } } } int ans=0; rep(i,1,c) rep(j,1,c)ans=max(ans,dp[i][j].ans); return ans; } int main() { while(~RII(n,c))printf("%d\n",sol()); return 0; }