CodeForces 492C Vanya and Exams【贪心】(水题)

题目链接:https://vjudge.net/contest/194475#problem/D

题目大意:
有一个人有n门课程,每一门课程他最多获得r学分,他只要所有课程的平均学分有avg,他就可以获得奖学金,每门课程,他已经获得了ai学分,剩下的每一个学分,都需要写bi篇论文才能得到,然后问你,这个人最少写多少论文才能获得奖学金

水题,毫无技巧可言

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long 
ll n, r, avg;

struct node
{
    ll score, res;
}nod[100100]; ;

bool mycmp(node a, node b)
{
    return a.res < b.res;
}

int main()
{
    ll i, j;
    while (scanf("%lld%lld%lld", &n, &r, &avg)!= EOF)
    {
        memset(nod, 0, sizeof(nod));
        ll target = n * avg;          //将要达到的平均分转换为要达到的总目标分,这要比较好判断
        ll sum=0,ans=0;
        for (i = 0; i < n; i++)
        {
            scanf("%lld%lld", &nod[i].score, &nod[i].res);
            sum += nod[i].score;
        }
        if (sum >= target)
        {
            printf("0\n");
        }
        else
        {
            sort(nod, nod + n, mycmp);
            for (i = 0; i < n; i++)
            {
                if (nod[i].score == r)continue;       //如果该点已经是该科目的最高分了
                ll cur = r - nod[i].score;
                if ((sum + cur) <= target) 
                {
                    sum += cur;
                    ans += cur * nod[i].res;
                }
                else
                {
                    ans += (target - sum)*nod[i].res;
                    sum = target;
                }
                if (sum == target)break;
            }
            printf("%lld\n", ans);
        }
    }
    return 0;
}

2018-04-21

猜你喜欢

转载自www.cnblogs.com/00isok/p/8903079.html