贪心 sort

//Geeksun 2017.12.05
//博主第一次使用C++  调试的时候sort()总是直接跳到库函数的页面,才疏学浅,望有高手能指点一二。


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 4
5 2
4 7
3 1
3 2
2 5
output
4
input
2 5 4
5 2
5 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篇论文,由于,他比较懒, 所以让你计算最少他需要写多少篇论文才能获得奖学金。


#include <stdio.h>
#include <algorithm>
using namespace std;
struct score
{
    int a;
    int b;
};
bool cmp(struct score x,struct score y);
struct score num[100005];
int main()
{
    long long n,r,avg,sum = 0;
    long long sum_poet = 0;
    int i;
    scanf("%I64d %I64d %I64d",&n,&r,&avg);
    for(i = 0; i < n; i++)
    {
        scanf("%d %d",&num[i].a,&num[i].b);
        sum += num[i].a;
    }
    sort(num,num + n,cmp);
    int flag = 0;
    i = 0;
    for(int i = 0;i < n&&sum < avg * n;i++)
    {
        flag = 1;
        long long int t = min(avg * n - sum,r - num[i].a);
        sum += t;
        sum_poet += t * num[i].b;
    }
    if(flag)
        printf("%I64d\n",sum_poet);
    else
        printf("0\n");
    return 0;
}
bool cmp(struct score x,struct score y)
{
    return x.b < y.b;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值