allcoding1_official: post #18929 — TG.ME

Car code

int solve(int T, int capacity, vector<vector<int>>& trips) {
map<int, long long> passenger_diffs;

for (int idx = 0; idx < T; idx++) {
int count = trips[idx][0];
int start_loc = trips[idx][1];
int end_loc = trips[idx][2];

passenger_diffs[start_loc] += count;
passenger_diffs[end_loc] -= count;
}

long long accumulated_standing_cost = 0;
long long active_riders = 0;
int last_point = -1;

for (auto& entry : passenger_diffs) {
int curr_point = entry.first;

if (last_point != -1 && curr_point > last_point) {
long long dist = curr_point - last_point;
if (active_riders > capacity) {
accumulated_standing_cost += (active_riders - capacity) * dist;
}
}

active_riders += entry.second;
last_point = curr_point;
}

return accumulated_standing_cost;
}
August 2, 2026 1.2K