HDU4882-ZCC Loves Codefires

题意:依次给出每题解出的时间,以及K值,求最小的失分


思路:贪心,因为每当做完一道题目,所失去的分数还要加上前面已经做完的题目*本身的K值,之后所有相加,如Hint所见。所以要保证失分最小就要让失分率大的先完成(time / K)。排序之后相加就是最后最小的失分。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 100005;

struct problem{
    int time, num;
    double k;
}p[MAXN];

int n;
long long ans = 0, sum = 0;

double cmp(problem a, problem b) {
    return a.k > b.k;
}

int main() {
    scanf("%d", &n);    
    for (int i = 0; i < n; i++)
        scanf("%d", &p[i].time);
    for (int i = 0; i < n; i++)
        scanf("%d", &p[i].num);
    for (int i = 0; i < n; i++)
        p[i].k = p[i].num / (p[i].time * 1.0);
    sort(p, p + n, cmp);
    for (int i = 0; i < n; i++) {
        sum += p[i].time;
        ans += sum * p[i].num; 
    }

    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(HDU4882-ZCC Loves Codefires)