Advertisement
Guest User

Untitled

a guest
Oct 9th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <cstdio>
  8.  
  9. using namespace std;
  10.  
  11. int A[111111];
  12. int D[111111];
  13. int P[111111];
  14. int a=-100,b=-100,c=-100;
  15. int pa=111110,pb=111110,pc=111110;
  16.  
  17. inline void srt() {
  18.   if (D[pa] > D[pc]) {
  19.     swap(pa,pc);
  20.     swap(a,c);
  21.   }
  22.   if (D[pb] > D[pc]) {
  23.     swap(pb,pc);
  24.     swap(b,c);
  25.   }
  26.   if (D[pa] > D[pb]) {
  27.     swap(pa,pb);
  28.     swap(a,b);
  29.   }
  30. }
  31.  
  32. int main(void) {
  33.   int n;
  34.   int ans = 1, st = 0;
  35.   freopen("input.txt", "rt", stdin);
  36.   freopen("output.txt", "wt", stdout);
  37.   cin >> n;
  38.   for (int i = 0; i < n; i++) {
  39.     cin >> A[i];
  40.   }
  41.   D[0] = 1;
  42.   P[0] = 111110;
  43.   D[111110] = -1000000;
  44.   c = A[0];
  45.   pc = 0;
  46.   for (int i = 1; i < n; i++) {
  47.     if (abs(A[i]-c) != 1) {
  48.       D[i] = (pc == 111110 ? 0 : D[pc]) + 1;
  49.       P[i] = pc;
  50.     } else if (abs(A[i]-b) != 1) {
  51.       D[i] = (pb == 111110 ? 0 : D[pb]) + 1;
  52.       P[i] = pb;
  53.     } else {
  54.       D[i] = (pa == 111110 ? 0 : D[pa]) + 1;
  55.       P[i] = pa;
  56.     }
  57.     if (A[i] == a) {
  58.       pa = i;
  59.     } else if (A[i] == b) {
  60.       pb = i;
  61.     } else if (A[i] == c) {
  62.       pc = i;
  63.     } else {
  64.       if (b == 111110) {
  65.         pb = i;
  66.         b = A[i];
  67.       } else if (a == 111110 || D[pa] < D[i]) {
  68.         pa = i;
  69.         a = A[i];
  70.       }
  71.     }
  72.     srt();
  73.     if (D[i] > ans) {
  74.       ans = D[i];
  75.       st = i;
  76.     }
  77.   }
  78.   cout << n-ans << endl;
  79.   vector<int> R;
  80.   while (st != 111110) {
  81.     R.push_back(A[st]);
  82.     st = P[st];
  83.   }
  84.   for (int i = (int)R.size()-1; i >= 0; i--) {
  85.     cout << R[i] << " ";
  86.   }
  87.   cout << endl;
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement