CodeForces 492C Vanya and Exams

来源:互联网 发布:海拉淘宝店 编辑:程序博客网 时间:2024/05/16 15:53

CodeForces 492C Vanya and Exams 
CodeForces 492C

题目大意: 
有一位同学要参加N门科目的考试,每门科目的满分固定,现在这位同学为了拿到奖学金需要达到平均分S,这就需要它在原来的基础上对某些科目的分数进行提升,然后提升每门科目需要做Ai个测试,于是希望能够做尽量少的测试能够使得分数达到平均分。

解题思路: 
将所有的科目按照对应的测试数进行排序,每次都选择测试数小的来进行分数的提升,直到到达平均分为止。

代码:

#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5 + 5;struct exam {    int a, b;}e[maxn];int cmp(const exam& e1, const exam& e2) {    return e1.b < e2.b; }int N, R, AVG;int main () {    ll gap, ans;    while (scanf("%d%d%d", &N, &R, &AVG) != EOF){        ans = gap = 0;        for (int i = 0; i < N; i++) {            scanf ("%d%d", &e[i].a, &e[i].b);            gap += e[i].a - AVG;        }        if (gap >= 0) {            printf ("%lld\n", ans);             continue;        }        sort(e, e + N, cmp);        for (int i = 0; i < N; i++) {            if (gap + (R - e[i].a) <= 0) {                ans += (ll)e[i].b * (R - e[i].a);                gap += (R - e[i].a);            } else {                ans -= (ll)e[i].b * gap;                gap = 0;            }            if (gap == 0)                break;        }        printf ("%lld\n", ans);    }    return 0;}
0 0
原创粉丝点击