fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. string s;
  9. getline(cin, s);
  10.  
  11. // =====================
  12. // BƯỚC 1: TÁCH SỐ
  13. // =====================
  14. stringstream ss(s);
  15. string word;
  16. vector<long long> a;
  17.  
  18. while (ss >> word) {
  19. string num = "";
  20. for (char c : word)
  21. if (isdigit(c))
  22. num += c;
  23.  
  24. if (!num.empty())
  25. a.push_back(stoll(num));
  26. }
  27.  
  28. // In dòng 1
  29. for (long long x : a)
  30. cout << x << " ";
  31. cout << "\n";
  32.  
  33. int n = a.size();
  34. if (n == 0) {
  35. cout << "0 0";
  36. return 0;
  37. }
  38.  
  39. // =====================
  40. // BƯỚC 2: DP
  41. // =====================
  42.  
  43. int bestLen = 1;
  44. int bestD = 0;
  45.  
  46. // thử d từ 0 → 10
  47. for (int d = 0; d <= 10; d++) {
  48.  
  49. unordered_map<long long, int> mp;
  50. int curMax = 1;
  51.  
  52. // duyệt từ phải sang trái
  53. for (int i = n - 1; i >= 0; i--) {
  54.  
  55. long long val = a[i] + d;
  56.  
  57. int len = 1;
  58. if (mp.count(val))
  59. len = mp[val] + 1;
  60.  
  61. // cập nhật tại giá trị a[i]
  62. if (!mp.count(a[i]))
  63. mp[a[i]] = len;
  64. else
  65. mp[a[i]] = max(mp[a[i]], len);
  66.  
  67. curMax = max(curMax, len);
  68. }
  69.  
  70. if (curMax > bestLen ||
  71. (curMax == bestLen && d < bestD)) {
  72. bestLen = curMax;
  73. bestD = d;
  74. }
  75. }
  76.  
  77. cout << bestLen << " " << bestD;
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0 0