Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <map>
- #include <algorithm>
- using namespace std;
- typedef vector<int> t_positions;
- typedef pair<int, int> t_interval;
- void solve() {
- int N;
- cin >> N;
- vector<int> A(N);
- for (int i = 0; i < N; ++i) {
- cin >> A[i];
- }
- map<int, t_positions> key_to_pos;
- for (int i = 0; i < N; ++i) {
- key_to_pos[A[i]].push_back(i);
- }
- vector<t_interval> intervals;
- map<t_interval, int> interval_to_count;
- for (map<int, t_positions>::const_iterator it = key_to_pos.begin(); it != key_to_pos.end(); ++it) {
- t_positions pos = it->second;
- sort(pos.begin(), pos.end());
- t_interval interval = make_pair(*pos.begin(), *pos.rbegin());
- intervals.push_back(interval);
- interval_to_count[interval] = pos.size();
- }
- sort(intervals.begin(), intervals.end());
- vector<int> best(intervals.size());
- for (int i = 0; i < intervals.size(); ++i) {
- const t_interval& interval = intervals[i];
- best[i] = interval_to_count[interval];
- for (int j = 0; j < i; ++j) {
- if (intervals[j].second < interval.first) {
- best[i] = max(best[i], best[j] + interval_to_count[interval]);
- }
- }
- }
- int result = *max_element(best.begin(), best.end());
- cout << result << endl;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement