P2672 推销员

贪心,水题

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
struct house
{
    int s;
    int a;
    int value;
}hs[maxn];
bool operator < (house a, house b) {
    return a.value<b.value;
}
int main() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> hs[i].s;
    }
    for(int i = 1; i <= n; i++) {
        cin >> hs[i].a;
        hs[i].value = hs[i].s * 2 + hs[i].a;
    }
    sort(hs+1, hs+n+1);
    cout << hs[n].value << endl;
    int changed = true;
    int last = n;
    for(int i = 2; i <= n; i++) {
        if(changed) {
            for(int j = 1; j <= n; j++) {
                if(hs[j].s < hs[last].s) {
                    hs[j].value = hs[j].a;
                }
                else if(hs[j].s > hs[last].s) {
                    hs[j].value = hs[j].a + (hs[j].s-hs[last].s)*2;
                }
            }
            sort(hs, hs+n);
            changed = false;
        }
        int ans = 0;
        for(int j = 1; j <= i; j++) {
            ans += hs[n-j+1].value;
            if(hs[n-j+1].s > last) {
                changed = true;
                last = n-j+1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gengchen/p/6023606.html