Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef vector<int> vi;
  6. typedef pair<int,int> pi;
  7. #define F first
  8. #define S second
  9. #define PB push_back
  10. #define MP make_pair
  11. const ll OO = (ll) 1e9+7;
  12. map<ll,ll> m;
  13. vector<ll> seq;
  14. int main()
  15. {
  16.     // input 2 1 2 2 3 1 2 3 output 3
  17.     ios_base::sync_with_stdio(false);
  18.     cin.tie(NULL);
  19.     cin.tie(NULL);
  20.     freopen("smallest.in","r",stdin);
  21.     int t,n;
  22.     cin>>t;
  23.     while(t--){
  24.     m.clear();
  25.     seq.clear();
  26.     cin>>n;
  27.     int ans=n;
  28.     for(int i=0;i<n;i++){
  29.         int tmp;
  30.         cin>>tmp;
  31.         m[tmp]++;
  32.         seq.PB(tmp);
  33.     }
  34.     int j=n-1;
  35.     for(int i=0;i<n;i++){
  36.             if(i>j) break;
  37.             m[seq[i-1]]--;
  38.     while((j<n-1)&&(m[seq[i-1]] == 0)){
  39.         j++;
  40.         m[seq[j]]++;
  41.     }
  42.     if(m[seq[i-1]] == 0)
  43.         break;
  44.     while((i<j)&&(m[seq[j]] > 1)){
  45.         if (m[seq[j]] > 1){
  46.                 m[seq[j]]--;
  47.                 j--;
  48.         }
  49.         else
  50.             break;
  51.         }
  52.         while((i<j)&&(m[seq[i]] > 1)){
  53.         m[seq[i]]--;
  54.         i++;
  55.         }
  56.     ans = min(j-i+1,ans);
  57.     }
  58.     cout<<ans<<endl;
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement