Codeforces Round #280 (Div. 2)-C. Vanya and Exams (贪心)_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:53:06
原创
1422人浏览过

Vanya and Exams

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade ai for the i-th exam. To increase the grade for the i-th exam by 1 point, Vanya must write bi essays. He can raise the exam grade multiple times.

What is the minimum number of essays that Vanya needs to write to get scholarship?

Input

The first line contains three integers n, r, avg (1?≤?n?≤?105, 1?≤?r?≤?109, 1?≤?avg?≤?min(r,?106)) ? the number of exams, the maximum grade and the required grade point average, respectively.

Each of the following n lines contains space-separated integers ai and bi (1?≤?ai?≤?r, 1?≤?bi?≤?106).

立即学习前端免费学习笔记(深入)”;

Output

In the first line print the minimum number of essays.

Sample test(s)

input

5 5 45 24 73 13 22 5
登录后复制

output

input

2 5 45 25 2
登录后复制

output

Note

In the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.

In the second sample, Vanya doesn't need to write any essays as his general point average already is above average.






题意:Vanya修了n门课,第i门课的学分是ai,已知每门课的最高学分不能超过r,而且他想得奖学金,但是获得奖学金的要求是:他的所有课的平均学分不得低于avg,如果不满足的话,他就不得不写论文去增加自己的学分,已知第i门课要增加1学分的话,必须写bi篇论文,由于,他比较懒, 所以让你计算最少他需要写多少篇论文才能获得奖学金。


分析:贪心。这个就是多了个限定条件??每门课的学分不能超过r,我们可以判断他的平均学分是不是满足条件了,若是,则直接输出0即可;否则,将所有课按bi排序,每次都在学分不超过r的条件下,去选择bi最小的那门课去修,直到学分等于r了,我们就再去选择当前已得学分小于r的且bi最小的去修,直到他的学分满足条件即可。这样,我们得到的结果,就是最小的结果。




AC代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7ffffffftypedef struct{    long long a, b;}node;node m[100005];int cmp(node x, node y){    return x.b < y.b;}int main(){    #ifdef sxk        freopen("in.txt","r",stdin);    #endif    long long n, r, avg;                           //要开long long    while(scanf("%lld%lld%lld",&n, &r, &avg)!=EOF)    {        long long sum = 0, ans = 0;        for(int i=0; i<n; i++){            scanf("%d%d", &m[i].a, &m[i].b);            sum += m[i].a;        }        if(n*r <= sum){            ans = 0;        }        long long foo = n*avg - sum;        sort(m, m+n, cmp);        long long k = 0;        for(long long i=0; i<n && k<foo; ){            if(m[i].a >= r) i++;            else{                int x = min(r - m[i].a, foo-k);                ans += x * m[i].b;                m[i].a += x;                k += x;            }        }        printf("%lld\n", ans);    }    return 0;}
登录后复制



心得:开始上来直接贪心 + 暴力,结果在第13个样例上超时了,所以就优化了一下。还是最优的代码好呀。一定不能得过且过,有好的方法,如果不是太麻烦的话,还是优化的好^_^



HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号