fork download
  1. /// UVA 10901 - Ferry Loading III
  2. #include <bits/stdc++.h>
  3. #define X first
  4. #define Y second
  5. #define pb push_back
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair <int,int> ii;
  9. typedef pair <int,ii> i_ii;
  10. typedef vector <int> vi;
  11. typedef vector <ii> vii;
  12.  
  13. const int maxn=102;
  14. const ll MOD=1000000007;
  15.  
  16. int T;
  17. int n,t,m,pos,x,curTime,curCar;
  18. string s;
  19. vector <int> Res;
  20.  
  21. int main()
  22. {
  23. // freopen("nhap.inp", "r", stdin);
  24. cin >> T;
  25. for (int test = 1; test <= T; test++)
  26. {
  27. cin >> n >> t >> m;
  28. curTime = curCar = pos = 0;
  29. queue <ii> L;
  30. queue <ii> R;
  31. Res.assign(m+1,0);
  32.  
  33. for (int i = 1; i <= m; i++)
  34. {
  35. cin >> x >> s;
  36. if (s[0] == 'l')
  37. L.push(ii(x,i));
  38. else R.push(ii(x,i));
  39. }
  40. while (curCar < m)
  41. {
  42. bool checkL = false, checkR = false;
  43.  
  44. if (pos == 0)
  45. {
  46. for (int i = 1; i <= n; i++)
  47. {
  48. if (L.empty() || L.front().X > curTime) break;
  49. Res[L.front().Y] = curTime + t;
  50. L.pop();
  51. checkL = true;
  52. curCar++;
  53. }
  54. if (checkL)
  55. {
  56. pos = 1;
  57. curTime += t;
  58. }
  59. else
  60. {
  61. if (!L.empty() && !R.empty())
  62. {
  63. if (L.front().X <= R.front().X)
  64. curTime = L.front().X;
  65. else curTime = max(curTime,R.front().X) + t, pos=1;
  66. }
  67. else if (L.empty())
  68. {
  69. if (R.front().X <= curTime) curTime += t;
  70. else curTime = R.front().X + t;
  71. pos=1;
  72. }
  73. else curTime = L.front().X;
  74. }
  75. }
  76. else
  77. {
  78. for (int i = 1; i <= n; i++)
  79. {
  80. if (R.empty() || R.front().X > curTime) break;
  81. Res[R.front().Y] = curTime + t;
  82. R.pop();
  83. checkR = true;
  84. curCar++;
  85. }
  86. if (checkR)
  87. {
  88. pos = 0;
  89. curTime += t;
  90. }
  91. else
  92. {
  93. if (!L.empty() && !R.empty())
  94. {
  95. if (L.front().X >= R.front().X)
  96. curTime = R.front().X;
  97. else curTime = max(curTime,L.front().X) + t, pos=0;
  98. }
  99. else if (R.empty())
  100. {
  101. if (L.front().X <= curTime) curTime += t;
  102. else curTime = L.front().X + t;
  103. pos=0;
  104. }
  105. else curTime = R.front().X;
  106. }
  107. }
  108. }
  109. for (int i = 1; i <= m; i++)
  110. printf("%d\n",Res[i]);
  111. if (test != T) cout<<endl;
  112. }
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
Standard output is empty