Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://codeforces.com/contest/1223/problem/D
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int Q;
- cin >> Q;
- for(int q = 0; q < Q; ++q)
- {
- int n;
- cin >> n;
- vector<int> a(n), first(n, n), last(n, -1);
- for(int i = 0; i < n; ++i)
- {
- cin >> a[i];
- a[i]--;
- first[a[i]] = min(first[a[i]], i);
- last[a[i]] = max(last[a[i]], i);
- }
- vector<int> unique;
- for(int i = 0; i < n; ++i)
- {
- if(last[i] != -1) unique.push_back(i);
- }
- int current = 1;
- int ans = 1;
- for(int i = 0; i < unique.size() - 1; ++i)
- {
- if(last[unique[i]] < first[unique[i + 1]])
- {
- current++;
- ans = max(ans, current);
- }
- else current = 1;
- }
- cout << unique.size() - ans << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement