贪心算法B 最小费用流 vanya ,exam

题目描述:

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.

Examples

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.

题目大意:

ai是每门课的分数,avg是目标平均分,给第i门课提高1分需要写bi篇论文,问最少写多少篇论文可以使平均分达到avg

思路:

这种贪心算法的题目,首先应该确定排序所用的条件,用结构体保存条件,方便整体排序

这个题目,数据类型一定要用Long long 啊

将所有课按bi递减排序,然后不超过r的条件下,去选择bi最小的那门课去修,直到学分等于r了;每次得到分数后还需要判断是否达到目标总分。

在代码中我注释掉了一段代码,那样子写会超时,因为每次都只加一

代码:

#include<iostream>
#include<algorithm>
#define N 100005
using namespace std;

struct grade{
    long long  ai;
    long long  bi;
}g[N];

bool cmp(grade A,grade B){ //按bi递减排序
    if(A.bi<B.bi) return true;
    else return false;
}

long long minpapers(grade *g,int n,long long r,long long avg)
{
    long long k=0;
    long long sum=0;
    for(int i=0;i<n;i++){
        sum+=g[i].ai;
    }//先求目前的总分数

    if(sum>=(n*avg)) return 0;//如果目前的总分数超过要求的总分数返回0

    else{//处理小于的情况
        int i=0;
        long long remain=(n*avg)-sum;//把目标总分和现在的分数的差值记录下来
        long long m=0;
        while(m<remain){//循环终止的条件是等于目标分值
            if(g[i].ai<r){
                 long long ans = min(r - g[i].ai, remain-m);//这样写可以节省时间,把需要论文少的尽量加满
                k+= ans* g[i].bi;//所需论文数
                g[i].ai += ans;
                m+=ans;

            }
            else i++;
        }

        /*while(sum<(n*avg)){//循环终止的条件是等于要求的总分数
            if(g[i].ai<r) {//上限是r
                k+=g[i].bi;//每次的论文数
                sum+=1;
                g[i].ai+=1;
            }
            else if(g[i].ai==r) i++;
        }*/
        return k;
    }
}
int main()
{
    int n;
    long long r,avg;
    while(cin>>n>>r>>avg){
        for(int i=0;i<n;i++){
            cin>>g[i].ai>>g[i].bi;
        }
        sort(g,g+n,cmp);
        cout<<minpapers(g,n,r,avg);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值