CF#280 (Div. 2) C.(贪心)

来源:互联网 发布:中科院云计算的股票 编辑:程序博客网 时间:2024/05/14 22:22
C. Vanya and Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
题目链接:http://codeforces.com/contest/492/problem/C

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.


解题思路:

题目大意就是给你n门功课,每门功课的最大分数r,总的平均分avg。接下来给出n行,每行a代表该门功课实际得的分数,b代表发表b篇文章可以使a涨一分(最高不超过r)。

首先如果当前的平均分大于等于给出的平均分,那么直接输出0;否则要算出需要发表多少篇文章才可以到达平均分,即key = avg * n  - sum1,sum1为实际所有门功课的和,很容易推导出这个式子。

接下来按照b从小到大排个序,每次贪b小的,知道当前这门功课的分数等于r。不能再涨为止。

最后注意开成__int64,int会WA,第四组数据特别大·····

完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef __int64 LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 100001;LL n , r , avg;struct node{    LL a , b;}s[maxn];bool cmp(node x , node y){    if(x.b == y.b)        return x.a < y.a;    return x.b < y.b;}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    while(~scanf("%I64d%I64d%I64d",&n,&r,&avg))    {        LL sum1 = 0;        for(int i = 0 ; i < n ; i ++)        {            scanf("%I64d%I64d",&s[i].a , &s[i].b);            sum1 += s[i].a;        }        if(sum1 / (double) n - avg > EPS)        {           printf("0\n");            continue;        }        sort(s , s + n , cmp);        LL key = avg * n - sum1;        LL res = 0;        for(int i = 0 ; i < n ; i ++)        {            if(s[i].a >= r)                continue;            if(r - s[i].a <= key && key != 0)            {                res += (r - s[i].a) * s[i].b;                key -= r - s[i].a;            }            else            {                res += key * s[i].b;                break;            }        }        printf("%I64d\n",res);    }}


0 0