QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#787336 | #9730. Elevator II | qinsj | TL | 0ms | 0kb | C++20 | 3.0kb | 2024-11-27 11:10:43 | 2024-11-27 11:10:44 |
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define db(args...) {\
string _s = #args;replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << endl;}
void err(istream_iterator<string> it){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << ' ';
err(++it, args...);
}
void solve() {
int n, f;
cin >> n >> f;
auto F = f;
#define a3 array<int, 3>
vector<a3> a(n);
vector<int> l(n), r(n);
ll res = 0;
vector<int> p = {f};
int mxl = 0;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
mxl = max(mxl, l[i]);
p.push_back(r[i]);
res += r[i] - l[i];
a[i] = {l[i], r[i], i};
}
sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end());
p.push_back(2e9);
int m = p.size();
vector<int> add[m];
vector<int> del[m];
for (auto [l, r, id] : a) {
auto p1 = lower_bound(p.begin(), p.end(), l) - p.begin();
auto p2 = lower_bound(p.begin(), p.end(), r + 1) - p.begin();
add[p1].push_back(id);
del[p2].push_back(id);
}
vector<int> jump(m, -1);
multiset<pii> pq;
for (int i = 0; i < m; i++) {
for (auto id : add[i]) {
pq.insert({r[id], id});
}
for (auto id : del[i]) {
pq.extract({r[id], id});
}
if (pq.size()) {
jump[i] = (*pq.rbegin()).second;
}
}
// db(m);
// for (int i = 0; i < m; i++) {
// db(i, jump[i]);
// }
// for (int i = 0; i < m - 2; i++) assert(jump[i] != -1);
vector<int> visi(n);
sort(a.begin(), a.end());
vector<int> ans;
while (f < mxl) {
auto id = lower_bound(p.begin(), p.end(), f) - p.begin();
if (id == m - 2) break;
int nid = jump[id];
// assert(nid != -1);
if (nid == -1 && visi[nid]) {
auto costid = lower_bound(a.begin(), a.end(), a3{f + 1, 0, 0}) - a.begin();
visi[a[costid][2]] = 1;
ans.push_back(a[costid][2]);
res += a[costid][0] - f;
f = a[costid][1];
} else {
ans.push_back(nid);
visi[nid] = 1;
f = r[nid];
}
}
for (int i = n - 1; i >= 0; i--) {
if (!visi[a[i][2]]) ans.push_back(a[i][2]);
}
cout << res << '\n';
for (auto x : ans) {
cout << x + 1 << " \n" [x == ans.back()];
}
ll cor = 0;
for (auto x : ans) {
cor += r[x] - l[x];
cor += max(0, l[x] - F);
F = r[x];
}
assert(cor == res);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
/*
g++ 1.cpp -std=c++20 -o 1 && ./1 < in.txt > out.txt
*/
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
2 4 2 3 6 1 3 2 7 5 6 2 5 2 4 6 8