Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int64_t inf = 1000 * 1000 * 1000;
- int64_t inf64 = inf * inf;
- int64_t mod = 1e9 + 7;
- int64_t nll = 0;
- #define se second
- #define fi first
- int32_t main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- cerr << fixed << setprecision(8);
- //freopen("sum2.in", "r", stdin);
- //freopen("sum2.out", "w", stdout);
- srand(time(0));
- auto START_TIME = chrono::high_resolution_clock::now();
- double ttime = clock();
- int64_t n, p;
- cin >> n >> p;
- vector<pair<int64_t, int64_t> > a(n);
- map<int64_t, int64_t> mp;
- map<int64_t, int64_t> qq;
- vector<int64_t> ans(n);
- for (int64_t i = 0; i < n; ++i)
- {
- cin >> a[i].fi;
- a[i].se = i;
- }
- sort(a.begin(), a.end());
- for (int64_t i = 0; i < n; ++i)
- {
- mp[a[i].se] = i;
- qq[a[i].se] = a[i].fi;
- }
- vector<int64_t> used(n, 0);
- int64_t cur_time = a[0].fi;
- set<int64_t> idxes;
- set<pair<int64_t, int64_t> > alive;
- for (int64_t i = 0; i < n; ++i)
- alive.emplace(a[i].fi, a[i].se);
- int64_t last_idx = 0;
- for (int64_t i = 0; i < n; ++i)
- {
- //idxes.clear();
- int64_t tempidx = last_idx;
- for (int64_t j = last_idx; j < n; ++j)
- {
- if (cur_time >= a[j].fi)
- {
- tempidx = j;
- if (used[a[j].se] == 0)
- idxes.insert(a[j].se);
- }
- else
- break;
- }
- last_idx = tempidx;
- //cout << "ITER: " << i << " ";
- //for (auto &c : idxes)
- // cout << c << " ";
- //cout << endl;
- int64_t idx = -1;
- if (idxes.size())
- idx = *idxes.begin();
- else
- {
- idx = (*alive.begin()).se;
- //for (int64_t j = 0; j < n; ++j)
- //{
- // if (used[a[j].se] == 0)
- // {
- // idx = a[j].se;
- // break;
- // }
- //}
- }
- alive.erase(make_pair(qq[idx], idx));
- int64_t new_idx = mp[idx];
- used[a[new_idx].se] = 1;
- if (idxes.find(idx) != idxes.end())
- idxes.erase(idx);
- cur_time = max(cur_time, a[new_idx].fi) + p;
- ans[idx] = cur_time;
- //cout << idx << " " << cur_time << endl;
- }
- for (auto &c : ans)
- cout << c << " ";
- cerr << endl << chrono::duration<long double>(chrono::high_resolution_clock::now() - START_TIME).count() << " sec.";
- }
- /*
- 5 9
- A 2 3 2
- A 3 5 1
- A 4 5 2
- Q 1 3
- Q 2 2
- Q 3 4
- Q 4 5
- Q 5 5
- Q 1 5
- 10
- 1 2 1 1 1 1 1 1 1 1
- 5 2 3 4
- 5
- 0 24
- 100 35
- 150 50
- 200 75
- 250 150
- 5
- 107
- 143
- 152
- 170
- 150
- 4
- 6 15 10 42
- 3
- -2 5
- 10 4
- 5 8
- 4
- 4 10
- 4 -2
- 5 0
- 5 100
- 3 6
- 1 4
- 1 5
- 1 6
- 2 4
- 2 5
- 2 6
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement