Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. typedef long long ll;
  7. typedef pair<int, int> pii; //each condensed chunk is (term,freq)
  8.  
  9. int A[100005];
  10. vector<pii> cond;
  11. int DP[100005];
  12.  
  13. int main()
  14. {
  15. ios::sync_with_stdio(0);
  16. int n;
  17. cin >> n;
  18. for (int i = 0; i < n; i++) cin >> A[i];
  19. int prev = -1,count=0;
  20. for (int i = 0; i < n; i++) {
  21. if (A[i] != prev) {
  22. if (i!=0) cond.push_back(make_pair(prev, count));
  23. prev = A[i];
  24. count = 1;
  25. }
  26. else count++;
  27. }
  28. cond.push_back(make_pair(prev, count));
  29. //bottom-up dp on the max amount we can save, store best three to move on
  30. 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
  31. int last[100005]; //stores the last one used to be good
  32. memset(best, -1, sizeof best);
  33. memset(last, -1, sizeof best);
  34. best[0][0] = 0;
  35. best[0][1] = 1;
  36. int opt;
  37. for (int curr = 1; curr < n; curr++) {
  38. opt = 0;
  39. for (int j = 0; j < 3;j++) if (best[j][0]>=0) if (abs(A[curr]-A[best[j][0]]) != 1) {
  40. opt = max(best[j][1] + 1, opt);
  41. }
  42. //look for what best gave us best curr
  43. for (int j = 0; j < 3; j++) if (best[j][0] >= 0) if (abs(A[curr] - A[best[j][0]]) != 1) {
  44. if (best[j][1] + 1 == opt) last[curr] = best[j][0];
  45. }
  46. //update best
  47. bool repl = false;
  48. for (int j = 0; j < 3; j++) if (best[j][0] == curr) {
  49. best[j][1] = opt;
  50. repl = true;
  51. }
  52. if (!repl) {
  53. int small = 10000000;
  54. for (int j = 0; j < 3; j++) small = min(small, best[j][0]);
  55. for (int j = 0; j < 3; j++) if (best[j][0] == small) {
  56. best[j][0] = curr;
  57. best[j][1] = opt;
  58. break;
  59. }
  60. }
  61. //cout << i << ' ' << curr << endl;
  62. }
  63. int ans = n - max(max(best[0][1], best[1][1]), best[2][1]);
  64. cout << ans << endl;
  65. vector<int> toMake;
  66. for (int j = 0; j < 3; j++) {
  67. if (best[j][1] + ans == n) {
  68. int now = best[j][0];
  69. toMake.push_back(now);
  70. while (last[now] != -1) {
  71. toMake.push_back(last[now]);
  72. now = last[now];
  73. }
  74. break;
  75. }
  76. }
  77. reverse(toMake.begin(), toMake.end());
  78. int p;
  79. for (int j = 0; j < toMake.size() - 1; j++) {
  80. cout << A[toMake[j]] << ' ';
  81. }
  82. cout <<A[toMake[toMake.size()-1]]<< endl;
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement