fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n, k, l;
  10. cin >> n >> k >> l;
  11.  
  12. queue<int> q;
  13.  
  14. int t = 0;
  15.  
  16. for(int i = 0; i < n; i++){
  17. int x;
  18. cin >> x;
  19. q.push(x);
  20. }
  21.  
  22. int prev = INT_MIN / 3;
  23.  
  24. int dist = 0;
  25.  
  26. while(dist < l && q.size()){
  27. if(dist - prev < k){
  28. dist += k;
  29. continue;
  30. }
  31.  
  32. int x = q.front();
  33. q.pop();
  34. if(x >= dist){
  35. if(x - t <= dist){
  36. prev = dist;
  37. dist += k;
  38. }else{
  39. int rem = (x - t) - dist + 1;
  40. t += rem;
  41. prev = dist;
  42. dist += k;
  43. }
  44. }else{
  45. x += t;
  46. x = min(x, dist);
  47. if(dist - x < k){
  48. dist += k;
  49. }
  50. prev = x;
  51.  
  52. }
  53.  
  54. }
  55. if(dist < l){
  56. if(dist - prev < k){
  57. int tmp = k - (dist - prev) + 1;
  58. dist += k;
  59. t += tmp;
  60. prev += t;
  61.  
  62. }else{
  63. int tmp = (dist - prev) - k + 1;
  64. t += tmp;
  65. prev += t;
  66. dist += k;
  67. }
  68. }
  69.  
  70. if(dist < l){
  71. int g = ceil(1.0 * (l - dist) / k);
  72. t += g * k;
  73. }
  74. cout << 2 * t << "\n";
  75. }
  76.  
  77. int main(){
  78. ios_base::sync_with_stdio(false);
  79. cin.tie(nullptr);
  80.  
  81. int t = 1;
  82. cin >> t;
  83.  
  84. for(int i = 1; i <= t; i++){
  85. solve();
  86. }
  87. return 0;
  88. }
Success #stdin #stdout 0s 5280KB
stdin
9
1 3 5
0
3 2 5
2 5 5
1 10 10
10
10 1 10
0 1 2 3 4 5 6 7 8 9
2 1 2
0 0
2 1 2
0 2
2 1 3
0 2
2 2 4
1 1
9 12 54
3 3 8 24 25 27 29 34 53
stdout
2
6
22
0
2
4
6
4
8