fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. struct Line
  7. {
  8. int start{};
  9. int end{};
  10. int weight{};
  11. };
  12.  
  13. enum operation {TAKE, DROP};
  14.  
  15. int optimize(operation op, std::vector<Line>::iterator it, std::vector<Line>::iterator end)
  16. {
  17. int weight{op == TAKE ? it->weight : 0};
  18. int line_end{it->end};
  19.  
  20. if (op == DROP) ++it;
  21. else while ( ++it != end)
  22. if (it->start > line_end) break;
  23.  
  24. if (it == end) return weight;
  25.  
  26. return weight + std::max(optimize(TAKE, it, end),
  27. optimize(DROP, it, end));
  28. }
  29.  
  30. static const int size = 100;
  31.  
  32. int main()
  33. {
  34. std::ifstream fin{"input.txt"};
  35.  
  36. int N;
  37. fin >> N;
  38.  
  39. std::vector<Line> lines(size);
  40.  
  41. for (int i{}; i < N; i++)
  42. {
  43. int num;
  44. fin >> num;
  45. Line& line = lines[num - 1];
  46.  
  47. if (line.start == 0)
  48. line.start = i;
  49. line.end = i;
  50. line.weight++;
  51. }
  52.  
  53. fin.close();
  54.  
  55. std::sort(lines.begin(), lines.end(), [](const Line& a, const Line& b) {
  56. return b.start > a.start;
  57. });
  58.  
  59. int result{std::max(optimize(TAKE, lines.begin(), lines.end()),
  60. optimize(DROP, lines.begin(), lines.end()))
  61. };
  62.  
  63. std::ofstream fout{"output.txt"};
  64. fout << result;
  65. fout.close();
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Standard output is empty