1. 程式人生 > 實用技巧 >codeforces 1335 E2 思維

codeforces 1335 E2 思維

題意:和E1相同,但是資料範圍擴大了,字元的數量為200,長度為2e5,如果按照E1的做法,時間複雜度為2e5*2e5*200,肯定T。

思路:把每一個字元出現的位置存進vector中,遍歷每一個字元,從兩邊取下標l,r。x已經確定,然後再遍歷200個字元從而確定y。

  雖然是三個for,但是前兩個for實際上時間複雜度為n,所以總時間複雜度為2e5 * 200.

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include 
<map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define
FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define
repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(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){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; int sum[210][maxn]; int n; void clearr() { rep(i,1,n){ rep(j,1,200){ sum[j][i] = 0; } } //rep(i,1,n) } int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { vector<int>pos[210]; cin>>n; clearr(); rep(i,1,n){ rep(j,1,200){ sum[j][i] = sum[j][i-1]; } int x; cin>>x; sum[x][i]++; pos[x].pb(i); } int ans = 0; rep(i,1,200) ans = max(ans,sum[i][n]); rep(i,1,200){ int l = 0,r = 0; int LEN = pos[i].size(); if(LEN <= 1) continue; rep(j,0,LEN/2-1){ l = pos[i][j]; r = pos[i][LEN-1-j]; int lenmid = 0; rep(j,1,200){ lenmid = max(lenmid,sum[j][r-1]-sum[j][l]); } ans = max(ans,lenmid+(j+1)*2); } cout<<ans<<endl; } }