fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //https://v...content-available-to-author-only...i.info/wiki/algo/basic/two-pointers.md
  5.  
  6.  
  7. //Hai dãy không giảm -> ghép thành 1 dãy không giảm, O(n+m)
  8. int i = 1, j = 1;
  9. vector<int> c;
  10. while (i <= n || j <= m){
  11. if (j == m + 1 || (i <= n && a[i] <= b[j]))
  12. c.push_back(a[i++]);
  13. else
  14. c.push_back(b[j++]);
  15. }
  16. for (auto it: c){
  17. cout << it << " ";
  18. }
  19.  
  20.  
  21.  
  22. //Mảng tăng dần -> tìm 2 phần tử vị trí khác nhau tổng là x
  23. int i = 1, j = N;
  24. while (i < j) {
  25. if (a[i] + a[j] == x) {
  26. cout << i << " " << j;
  27. return 0;
  28. }
  29. if (a[i] + a[j] < x)
  30. i += 1;
  31. else
  32. j -= 1;
  33. }
  34. cout << "No solution";
  35.  
  36.  
  37.  
  38. //Độ dài đoạn con dài nhất sao cho tổng các phần tử trong đoạn con không quá s
  39. int ans = 0, sum = 0;
  40. for (int l = 1, r = 1; r <= n; r++) {
  41. sum += a[r];
  42. while (sum > s) {
  43. sum -= a[l];
  44. l++;
  45. }
  46. ans = max(ans, r - l + 1);
  47. }
  48. cout << ans;
  49.  
  50.  
  51.  
  52. //Tortoise and Hare
  53.  
  54. //tortoise và hare gặp nhau
  55. int tortoise = 1, hare = 1;
  56. while (true) {
  57. tortoise = f(tortoise);
  58. hare = f(f(hare));
  59. if (tortoise == hare)
  60. break;
  61. }
  62. //tìm mu
  63. int mu = 0, p = 1;
  64. while (p != tortoise) {
  65. p = f(p);
  66. tortoise = f(tortoise);
  67. mu++;
  68. }
  69. //tìm lambda
  70. int lambda = 0;
  71. while (true) {
  72. lambda++;
  73. p = f(p);
  74. if (tortoise == p)
  75. break;
  76. }
Success #stdin #stdout 0.01s 5504KB
stdin
3
stdout
3