a53

balba

a53
Apr 23rd, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. // Bogdan Iordache
  2. // 100 de puncte
  3. // O(N*100)
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. bool is_better(const vector<int>& candidate, const vector<int>& curr) {
  8. if (candidate.size() > curr.size()) return true;
  9. if (candidate.size() < curr.size()) return false;
  10. for (int i = 0; i < (int)curr.size(); ++i)
  11. if (curr[i] < candidate[i]) return true;
  12. else if (curr[i] > candidate[i]) return false;
  13. return false;
  14. }
  15.  
  16. int main() {
  17. ifstream cin("balba.in");
  18. ofstream cout("balba.out");
  19.  
  20. int C, N;
  21. cin >> C >> N;
  22. assert(1 <= C && C <= 2);
  23. assert(1 <= N && N <= 100000);
  24.  
  25. vector<int> digits(N);
  26. for (int i = 0; i < N; ++i) {
  27. cin >> digits[i];
  28. assert(0 <= digits[i] && digits[i] <= 9);
  29. }
  30.  
  31. string str;
  32. assert(!(cin >> str));
  33.  
  34. if (C == 1) {
  35. int nr_seg = 1;
  36. int nr_seg_2 = 0;
  37. int curr_len = 1;
  38.  
  39. for (int i = 1; i < N; ++i) {
  40. if (digits[i] == digits[i - 1]) {
  41. curr_len++;
  42. } else {
  43. nr_seg++;
  44. if (curr_len > 1) nr_seg_2++;
  45. curr_len = 1;
  46. }
  47. }
  48. if (curr_len > 1) nr_seg_2++;
  49.  
  50. cout << nr_seg << '\n' << nr_seg_2;
  51. } else {
  52. vector<int> freq(10);
  53. for (int it : digits) freq[it]++;
  54.  
  55. vector<int> sol = vector<int>(), candidate = vector<int>(),
  56. aux = vector<int>();
  57. sol.reserve(N);
  58. candidate.reserve(N);
  59. aux.reserve(N);
  60. bool is_first, repeated;
  61. for (int repeat = -1; repeat <= 9; ++repeat) {
  62. for (int center = -1; center <= 9; ++center) {
  63. if (repeat == 9 && center == 9) {
  64. center = 9;
  65. }
  66. if (center != -1) {
  67. if (freq[center] == 0) continue;
  68. freq[center]--;
  69. }
  70. if (repeat != -1) {
  71. if (freq[repeat] < 3) {
  72. if (center != -1) freq[center]++;
  73. continue;
  74. }
  75. freq[repeat]--;
  76. }
  77.  
  78. is_first = true;
  79. for (int i = 9; i >= 0; --i) {
  80. if (freq[i] / 2 == 0) continue;
  81. if (i == 0 && is_first) break;
  82. is_first = false;
  83. for (int j = 0; j < freq[i] / 2; ++j) aux.push_back(i);
  84. }
  85.  
  86. repeated = false;
  87. for (int it : aux) {
  88. if (it == repeat && !repeated) {
  89. candidate.push_back(it);
  90. repeated = true;
  91. }
  92. candidate.push_back(it);
  93. }
  94. if (center != -1) candidate.push_back(center);
  95. reverse(aux.begin(), aux.end());
  96. for (int it : aux) candidate.push_back(it);
  97.  
  98. if (is_better(candidate, sol)) sol = candidate;
  99.  
  100. candidate.clear();
  101. reverse(aux.begin(), aux.end());
  102. for (int it : aux) candidate.push_back(it);
  103. if (center != -1) candidate.push_back(center);
  104. reverse(aux.begin(), aux.end());
  105. repeated = false;
  106. for (int it : aux) {
  107. if (it == repeat && !repeated) {
  108. candidate.push_back(it);
  109. repeated = true;
  110. }
  111. candidate.push_back(it);
  112. }
  113.  
  114. if (is_better(candidate, sol)) sol = candidate;
  115.  
  116. if (repeat != -1) freq[repeat]++;
  117. if (center != -1) freq[center]++;
  118. aux.clear();
  119. candidate.clear();
  120. }
  121. }
  122.  
  123. for (auto it : sol) cout << it;
  124. }
  125. string s;
  126. assert(!(cin >> s));
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment