QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#643452 | #6564. Frequent Flier | enze114514 | AC ✓ | 14ms | 14948kb | C++20 | 3.1kb | 2024-10-15 21:16:07 | 2024-10-15 21:16:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// Function to compute the minimum number of flights to pay for
long long min_paid_flights(int n, int m, long long k, const vector<long long> &flights) {
// Total padding at the start and end
int padding = m - 1;
// Create a padded flight list with m-1 zeros at both ends
vector<long long> f_padded;
f_padded.assign(padding, 0); // Padding at the start
f_padded.insert(f_padded.end(), flights.begin(), flights.end()); // Original flights
f_padded.insert(f_padded.end(), padding, 0); // Padding at the end
int n_padded = f_padded.size();
// Compute prefix sums of flights
vector<long long> prefix_flights(n_padded + 1, 0);
for(int i = 0; i < n_padded; ++i){
prefix_flights[i + 1] = prefix_flights[i] + f_padded[i];
}
// Initialize an array to track paid flights in each month
vector<long long> paid_flights(n_padded, 0);
long long total_paid = 0;
long long current_paid_in_window = 0;
// Iterate through each possible window
for(int i = 0; i <= n_padded - m; ++i){
// Update the current number of paid flights in the window
if(i > 0){
current_paid_in_window -= paid_flights[i -1];
}
current_paid_in_window += paid_flights[i + m -1];
// Total flights in the current window
long long total_flights = prefix_flights[i + m] - prefix_flights[i];
// Determine how many more flights need to be paid in this window
long long need_to_pay;
if(total_flights >= k){
need_to_pay = max((long long)k - current_paid_in_window, (long long)0);
}
else{
need_to_pay = max(total_flights - current_paid_in_window, (long long)0);
}
// If additional payments are needed, pay in the latest possible months within the window
if(need_to_pay > 0){
// Start from the last month in the window and move backwards
int j = i + m -1;
while(need_to_pay > 0 && j >= i){
// Calculate how many flights can be paid in month j
long long available = f_padded[j] - paid_flights[j];
if(available > 0){
// Pay as many as possible in this month, up to the need
long long pay_now = min(need_to_pay, available);
paid_flights[j] += pay_now;
current_paid_in_window += pay_now;
total_paid += pay_now;
need_to_pay -= pay_now;
}
j--;
}
// It's guaranteed by problem constraints that a solution exists
}
}
return total_paid;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
long long k;
cin >> n >> m >> k;
vector<long long> flights(n);
for(int i =0; i <n; ++i){
cin >> flights[i];
}
long long result = min_paid_flights(n, m, k, flights);
cout << result;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
8 3 2 3 1 4 1 5 9 2 6
output:
8
result:
ok single line: '8'
Test #2:
score: 0
Accepted
time: 12ms
memory: 10772kb
input:
200000 2467 999931035 182548858 69876218 33328350 919486767 739850600 948199964 392292320 39077742 366752074 917496841 246661698 37219034 56677740 188278971 965701828 28059790 13200243 825730775 542597589 320715170 939054378 470563899 914247467 990100491 290827128 903662394 611104879 18631185 412134...
output:
82994275905
result:
ok single line: '82994275905'
Test #3:
score: 0
Accepted
time: 0ms
memory: 12516kb
input:
1 200000 999959273 1255319
output:
1255319
result:
ok single line: '1255319'
Test #4:
score: 0
Accepted
time: 14ms
memory: 14948kb
input:
200000 118880 996878327 993340390 999483057 808153702 337349872 863222189 7495963 995920883 12950768 958082368 993215196 967152406 1388062 949959944 836952150 964071667 5291 139115263 958470154 51295691 175385 925242139 995685554 10895812 12563 55482 479983443 42805 996241239 6228013 302633329 10331...
output:
2990634981
result:
ok single line: '2990634981'
Test #5:
score: 0
Accepted
time: 12ms
memory: 9420kb
input:
200000 15 44049967 999927791 29351 999894087 687373343 518282902 19725626 452930 6701 999697321 27032796 5498 259987 999999980 485673990 805879 315651507 166801295 189865616 999910671 6203805 978801777 41916529 942904087 997589912 17146790 922137016 444882952 999884603 999926345 938979650 18597283 9...
output:
598769574112
result:
ok single line: '598769574112'