Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii; //each condensed chunk is (term,freq)
- int A[100005];
- vector<pii> cond;
- int DP[100005];
- int main()
- {
- ios::sync_with_stdio(0);
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) cin >> A[i];
- int prev = -1,count=0;
- for (int i = 0; i < n; i++) {
- if (A[i] != prev) {
- if (i!=0) cond.push_back(make_pair(prev, count));
- prev = A[i];
- count = 1;
- }
- else count++;
- }
- cond.push_back(make_pair(prev, count));
- //bottom-up dp on the max amount we can save, store best three to move on
- int best[3][2]; //stores the best i so far and how many it saved: best[j][0] is the index, best[j][1] is the amount save
- int last[100005]; //stores the last one used to be good
- memset(best, -1, sizeof best);
- memset(last, -1, sizeof best);
- best[0][0] = 0;
- best[0][1] = 1;
- int opt;
- for (int curr = 1; curr < n; curr++) {
- opt = 0;
- for (int j = 0; j < 3;j++) if (best[j][0]>=0) if (abs(A[curr]-A[best[j][0]]) != 1) {
- opt = max(best[j][1] + 1, opt);
- }
- //look for what best gave us best curr
- for (int j = 0; j < 3; j++) if (best[j][0] >= 0) if (abs(A[curr] - A[best[j][0]]) != 1) {
- if (best[j][1] + 1 == opt) last[curr] = best[j][0];
- }
- //update best
- bool repl = false;
- for (int j = 0; j < 3; j++) if (best[j][0] == curr) {
- best[j][1] = opt;
- repl = true;
- }
- if (!repl) {
- int small = 10000000;
- for (int j = 0; j < 3; j++) small = min(small, best[j][0]);
- for (int j = 0; j < 3; j++) if (best[j][0] == small) {
- best[j][0] = curr;
- best[j][1] = opt;
- break;
- }
- }
- //cout << i << ' ' << curr << endl;
- }
- int ans = n - max(max(best[0][1], best[1][1]), best[2][1]);
- cout << ans << endl;
- vector<int> toMake;
- for (int j = 0; j < 3; j++) {
- if (best[j][1] + ans == n) {
- int now = best[j][0];
- toMake.push_back(now);
- while (last[now] != -1) {
- toMake.push_back(last[now]);
- now = last[now];
- }
- break;
- }
- }
- reverse(toMake.begin(), toMake.end());
- int p;
- for (int j = 0; j < toMake.size() - 1; j++) {
- cout << A[toMake[j]] << ' ';
- }
- cout <<A[toMake[toMake.size()-1]]<< endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement