Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <ctime>
- #include <algorithm>
- #include <cstdlib>
- #include <vector>
- #include <cstdio>
- using namespace std;
- int A[111111];
- int D[111111];
- int P[111111];
- int a=-100,b=-100,c=-100;
- int pa=111110,pb=111110,pc=111110;
- inline void srt() {
- if (D[pa] > D[pc]) {
- swap(pa,pc);
- swap(a,c);
- }
- if (D[pb] > D[pc]) {
- swap(pb,pc);
- swap(b,c);
- }
- if (D[pa] > D[pb]) {
- swap(pa,pb);
- swap(a,b);
- }
- }
- int main(void) {
- int n;
- int ans = 1, st = 0;
- freopen("input.txt", "rt", stdin);
- freopen("output.txt", "wt", stdout);
- cin >> n;
- for (int i = 0; i < n; i++) {
- cin >> A[i];
- }
- D[0] = 1;
- P[0] = 111110;
- D[111110] = -1000000;
- c = A[0];
- pc = 0;
- for (int i = 1; i < n; i++) {
- if (abs(A[i]-c) != 1) {
- D[i] = (pc == 111110 ? 0 : D[pc]) + 1;
- P[i] = pc;
- } else if (abs(A[i]-b) != 1) {
- D[i] = (pb == 111110 ? 0 : D[pb]) + 1;
- P[i] = pb;
- } else {
- D[i] = (pa == 111110 ? 0 : D[pa]) + 1;
- P[i] = pa;
- }
- if (A[i] == a) {
- pa = i;
- } else if (A[i] == b) {
- pb = i;
- } else if (A[i] == c) {
- pc = i;
- } else {
- if (b == 111110) {
- pb = i;
- b = A[i];
- } else if (a == 111110 || D[pa] < D[i]) {
- pa = i;
- a = A[i];
- }
- }
- srt();
- if (D[i] > ans) {
- ans = D[i];
- st = i;
- }
- }
- cout << n-ans << endl;
- vector<int> R;
- while (st != 111110) {
- R.push_back(A[st]);
- st = P[st];
- }
- for (int i = (int)R.size()-1; i >= 0; i--) {
- cout << R[i] << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement