zoukankan      html  css  js  c++  java
  • CodeForces 492C Vanya and Exams (贪心)

    C. 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 ≤ 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 ≤ 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

    思路: 平均分其实乘一下n以后就是总分,也就是说总分要达到这个sum=avg*n,而每一科提升一分的代价是bi个论文,提升空间则还有r-ai个学分。计算好每个科目以后,贪心取代价最小的,按bi从小到大排序,能取多少就取多少,直到分数达到sum为止,性价比最高的科目的提分空间用完以后再去性价比第二高的科目。

    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> pii;
    const int INF = 1e9;
    const double eps = 1e-6;
    //const int N = ;
    int cas = 1;
    
    ll n,r,avg,sum;
    
    void run()
    {
        ll a,b;
        vector<pii> v;
        sum = avg * n;
        for(int i = 0; i < n; i++ )
        {
            cin >> a >> b;
            sum -= a;
            v.push_back(make_pair(b,r-a));
        }
        sort(v.begin(),v.end());
        ll ans = 0;
        for(int i = 0; i < n; i++ )
        {
            if(sum <= 0) break;
            if(sum >= v[i].second)
                ans+=v[i].first*v[i].second, sum-=v[i].second;
            else
                ans += sum*v[i].first, sum -= sum;
        }
        cout << ans << endl;
    }
    
    int main()
    {
        #ifdef LOCAL
    //    freopen("case.txt","r",stdin);
        #endif
        while(cin >> n >> r >> avg)
            run();
        return 0;
    }
  • 相关阅读:
    美达飞凡16X DVD起死回生记
    vs2k5 中asp.net "Web Site Administration Tool "使用中遇到的问题
    有关sqlserver的锁
    基于dotnet2.0的联通sgip1.2协议二级网关源码
    .net winform下TreeNode在没有子结点时也显示+号的解决办法
    小胜凭智, 大胜靠德
    寄语八十年代的新一代
    PHP+APACHE+MYSQL+WINDOWS 环境配置秘笈,一定行!!!!
    JS获取当前屏幕分辨率
    godaddy免费空间安装wordpress教程之500错误的解决办法/读写权限修改
  • 原文地址:https://www.cnblogs.com/someblue/p/4136429.html
Copyright © 2011-2022 走看看