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

来源:互联网 发布:linux系统dd命令 编辑:程序博客网 时间:2024/05/01 02:19

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 nravg (1 ≤ n ≤ 1051 ≤ r ≤ 1091 ≤ 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 ≤ r1 ≤ 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
4
input
2 5 45 25 2
output
0
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个样例上超时了,所以就优化了一下。还是最优的代码好呀。一定不能得过且过,有好的方法,如果不是太麻烦的话,还是优化的好^_^



0 0
原创粉丝点击