QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#874066 | #9730. Elevator II | yylx | WA | 0ms | 3584kb | C++14 | 3.2kb | 2025-01-27 13:31:38 | 2025-01-27 13:31:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// We'll store each passenger as a struct.
struct Person {
long long l, r; // start and destination floors
int idx; // original index (for output)
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
// We will handle up to T test cases. The sum of all n's can be up to 3e5.
while(T--){
long long n, f;
cin >> n >> f;
vector<Person> people(n);
for(int i = 0; i < n; i++){
cin >> people[i].l >> people[i].r;
people[i].idx = i+1; // Store 1-based index for output
}
// Sort by starting floor ascending
sort(people.begin(), people.end(),
[](auto &A, auto &B){
return A.l < B.l;
});
// Min-heap by destination floor r
// We'll pop the person with the smallest r when we can.
priority_queue<
Person,
vector<Person>,
function<bool(const Person&, const Person&)>
> pq([](auto &A, auto &B){
return A.r > B.r; // smallest r on top
});
long long currentFloor = f;
long long totalCost = 0;
int idx = 0; // index into "people" (sorted by l)
int served = 0; // how many we've handled
vector<int> answer; // will store the order of indices
answer.reserve(n);
// Process all n passengers
while(served < n){
// First, push all whose l <= currentFloor into the heap
while(idx < n && people[idx].l <= currentFloor){
pq.push(people[idx]);
idx++;
}
if(!pq.empty()){
// Take the one with smallest r
Person top = pq.top();
pq.pop();
// Cost to go from currentFloor up to top.l (if top.l > currentFloor)
if(top.l > currentFloor){
totalCost += (top.l - currentFloor);
currentFloor = top.l;
}
// Now we pick them up and move from l to r
totalCost += (top.r - top.l);
currentFloor = top.r;
answer.push_back(top.idx);
served++;
} else {
// The heap is empty but we haven't served everyone
// => we must jump to the next un-pushed passenger's l
if(idx < n){
long long nextL = people[idx].l;
// Move elevator up to nextL
if(nextL > currentFloor){
totalCost += (nextL - currentFloor);
currentFloor = nextL;
}
// Then we'll push that passenger (in the next loop iteration)
}
}
}
// Output the results
cout << totalCost << "\n";
// Print permutation
for(int i = 0; i < (int)answer.size(); i++){
cout << answer[i] << (i+1 < (int)answer.size() ? ' ' : '\n');
}
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3584kb
input:
2 4 2 3 6 1 3 2 7 5 6 2 5 2 4 6 8
output:
11 2 1 4 3 6 1 2
result:
wrong answer Participant's cost is 6, which is worse than jury's cost 5 (test case 2)