欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

程序员文章站 2022-05-21 23:29:48
...
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 #include #include #include #include #include #include #include #include #include #include #include using namespace std;#define INF 0x7ffffffftypedef struct{    long long a, b;}node;node m[100005];int cmp(node x, node y){    return x.b = 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个样例上超时了,所以就优化了一下。还是最优的代码好呀。一定不能得过且过,有好的方法,如果不是太麻烦的话,还是优化的好^_^