This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbPer, nbMatelas, T;
cin >> nbPer >> nbMatelas >> T;
vector<int> x(nbPer);
for (int i = 0; i < nbPer; ++i) {
cin >> x[i];
x[i] -= i;
}
deque<pair<int, int>> candidats;
candidats.emplace_back(-1e18, -1);
vector<int> f(nbPer);
for (int i = 0; i < nbPer; ++i) {
while (candidats.back().first >= x[i])
candidats.pop_back();
candidats.emplace_back(x[i], i);
auto it = lower_bound(candidats.begin(), candidats.end(),
pair<int, int>(T - i, 1e18));
it--;
f[i] = it->second;
}
dbg(f);
vector<pair<int, int>> intervalles;
int nbJamais = 0;
for (int i = 0; i < nbPer; ++i) {
if (f[i] == i)
continue;
if (f[i] == -1)
nbJamais++;
else
intervalles.emplace_back(f[i], i);
}
sort(intervalles.begin(), intervalles.end(), [&](auto l, auto r) {
if (l.first == r.first)
return l.second > r.second;
else
return l.first < r.first;
});
int nbInt = intervalles.size();
vector<int> stk;
vector<int> par(nbInt);
for (int i = 0; i < nbInt; ++i) {
auto [l, r] = intervalles[i];
while (!stk.empty() and intervalles[stk.back()].second < l)
stk.pop_back();
if (!stk.empty())
par[i] = stk.back();
else
par[i] = -1;
stk.push_back(i);
}
for (int i = 0; i < nbInt; ++i)
dbg(i, intervalles[i].first, intervalles[i].second, par[i]);
vector<int> depth(nbInt);
for (int i = 0; i < nbInt; ++i) {
if (par[i] != -1)
depth[i] = 1 + depth[par[i]];
}
vector<pair<int, int>> deepest(nbInt);
for (int i = nbInt - 1; i >= 0; --i) {
deepest[i] = max(deepest[i], pair(depth[i], i));
if (par[i] != -1)
deepest[par[i]] = max(deepest[par[i]], deepest[i]);
}
vector<vector<int>> childs(nbInt);
for (int i = 0; i < nbInt; ++i)
if (par[i] != -1)
childs[par[i]].push_back(i);
priority_queue<pair<int, int>> pq;
vector<bool> erased(nbInt);
for (int i = 0; i < nbInt; ++i)
if (par[i] == -1)
pq.emplace(deepest[i]);
int sol = nbPer - nbJamais;
for (int it = 0; it < nbMatelas and !pq.empty(); ++it) {
auto [cnt, u] = pq.top();
pq.pop();
sol -= cnt + 1;
while (u != -1 and !erased[u]) {
erased[u] = true;
for (int v : childs[u])
if (!erased[v])
pq.emplace(deepest[v].first - depth[v], deepest[v].second);
u = par[u];
}
}
cout << sol << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |