Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int64_t inf = 1000 * 1000 * 1000;
  6. int64_t inf64 = inf * inf;
  7. int64_t mod = 1e9 + 7;
  8. int64_t nll = 0;
  9.  
  10. #define se second
  11. #define fi first
  12.  
  13. int32_t main() {
  14. ios_base::sync_with_stdio(false);
  15. cin.tie(NULL);
  16. cout.tie(NULL);
  17. cerr << fixed << setprecision(8);
  18. //freopen("sum2.in", "r", stdin);
  19. //freopen("sum2.out", "w", stdout);
  20. srand(time(0));
  21. auto START_TIME = chrono::high_resolution_clock::now();
  22. double ttime = clock();
  23.  
  24. int64_t n, p;
  25. cin >> n >> p;
  26. vector<pair<int64_t, int64_t> > a(n);
  27. map<int64_t, int64_t> mp;
  28. map<int64_t, int64_t> qq;
  29. vector<int64_t> ans(n);
  30. for (int64_t i = 0; i < n; ++i)
  31. {
  32. cin >> a[i].fi;
  33. a[i].se = i;
  34. }
  35. sort(a.begin(), a.end());
  36. for (int64_t i = 0; i < n; ++i)
  37. {
  38. mp[a[i].se] = i;
  39. qq[a[i].se] = a[i].fi;
  40. }
  41.  
  42. vector<int64_t> used(n, 0);
  43. int64_t cur_time = a[0].fi;
  44. set<int64_t> idxes;
  45. set<pair<int64_t, int64_t> > alive;
  46. for (int64_t i = 0; i < n; ++i)
  47. alive.emplace(a[i].fi, a[i].se);
  48. int64_t last_idx = 0;
  49. for (int64_t i = 0; i < n; ++i)
  50. {
  51. //idxes.clear();
  52. int64_t tempidx = last_idx;
  53. for (int64_t j = last_idx; j < n; ++j)
  54. {
  55. if (cur_time >= a[j].fi)
  56. {
  57. tempidx = j;
  58. if (used[a[j].se] == 0)
  59. idxes.insert(a[j].se);
  60. }
  61. else
  62. break;
  63. }
  64. last_idx = tempidx;
  65. //cout << "ITER: " << i << " ";
  66. //for (auto &c : idxes)
  67. // cout << c << " ";
  68. //cout << endl;
  69. int64_t idx = -1;
  70. if (idxes.size())
  71. idx = *idxes.begin();
  72. else
  73. {
  74. idx = (*alive.begin()).se;
  75. //for (int64_t j = 0; j < n; ++j)
  76. //{
  77. // if (used[a[j].se] == 0)
  78. // {
  79. // idx = a[j].se;
  80. // break;
  81. // }
  82. //}
  83. }
  84. alive.erase(make_pair(qq[idx], idx));
  85. int64_t new_idx = mp[idx];
  86. used[a[new_idx].se] = 1;
  87. if (idxes.find(idx) != idxes.end())
  88. idxes.erase(idx);
  89. cur_time = max(cur_time, a[new_idx].fi) + p;
  90. ans[idx] = cur_time;
  91. //cout << idx << " " << cur_time << endl;
  92. }
  93. for (auto &c : ans)
  94. cout << c << " ";
  95.  
  96. cerr << endl << chrono::duration<long double>(chrono::high_resolution_clock::now() - START_TIME).count() << " sec.";
  97. }
  98. /*
  99. 5 9
  100. A 2 3 2
  101. A 3 5 1
  102. A 4 5 2
  103. Q 1 3
  104. Q 2 2
  105. Q 3 4
  106. Q 4 5
  107. Q 5 5
  108. Q 1 5
  109.  
  110. 10
  111. 1 2 1 1 1 1 1 1 1 1
  112.  
  113. 5 2 3 4
  114.  
  115. 5
  116. 0 24
  117. 100 35
  118. 150 50
  119. 200 75
  120. 250 150
  121. 5
  122. 107
  123. 143
  124. 152
  125. 170
  126. 150
  127.  
  128. 4
  129. 6 15 10 42
  130. 3
  131. -2 5
  132. 10 4
  133. 5 8
  134. 4
  135. 4 10
  136. 4 -2
  137. 5 0
  138. 5 100
  139.  
  140. 3 6
  141. 1 4
  142. 1 5
  143. 1 6
  144. 2 4
  145. 2 5
  146. 2 6
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement