【題解】UVA 11389 The Bus Driver Problem

【題目敘述】https://vjudge.net/problem/UVA-11389
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e538
【解題想法】Greedy

#include <bits/stdc++.h>
using namespace std;

int n, d, r, a[105], b[105], ans;

int main(){
    while (cin >> n >> d >> r){
        if (n == 0) break;
        for (int i = 0; i < n; i++){
            cin >> a[i];
        }
        for (int i = 0; i < n; i++){
            cin >> b[i];
        }
        sort(a, a+n);
        sort(b, b+n);
        reverse(b, b+n);
        ans = 0;
        for (int i = 0; i < n; i++){
            if (a[i] + b[i] > d){
                ans += r * (a[i] + b[i] - d);
            }
        }
        cout << ans << "\n";
    }
}
分享本文 Share with friends