CodeForces 492C Vanya and Exams(贪心)
2016-05-17 23:03
381 查看
题意:有一个人有n门课程,每一门课程他最多获得r学分,他只要所有课程的平均学分有avg,他就可以获得奖学金,每门课程,他已经获得了ai学分,剩下的每一个学分,都需要写bi篇论文才能得到,然后问你,这个人最少写多少论文才能获得奖学金
思路:贪心,我们选择bi最小的开始写论文,然后扫一遍就好了,直到学分够为止
Description
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.
Sample Input
Input
Output
Input
Output
Hint
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.
思路:贪心,我们选择bi最小的开始写论文,然后扫一遍就好了,直到学分够为止
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; #define maxn 100005 pair<long long,long long> p[maxn]; int main() { int n; long long r,avg; scanf("%d%lld%lld",&n,&r,&avg); avg*=n; for(int i=0;i<n;i++) { scanf("%lld%lld",&p[i].second,&p[i].first); avg-=p[i].second; p[i].second = r - p[i].second; } sort(p,p+n); long long ans = 0; for(int i=0;i<n;i++) { if(avg<=0)break; if(p[i].second>=avg) { ans+=avg*p[i].first; break; } else { ans+=p[i].second*p[i].first; avg-=p[i].second; } } printf("%lld\n",ans); }
Description
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.
Sample Input
Input
5 5 4 5 2 4 7 3 1 3 2 2 5
Output
4
Input
2 5 45 2
5 2
Output
0
Hint
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.
相关文章推荐
- 1.Redis简介与安装
- 【转载】D3D中的Texture应用示例
- Navicat Premium (patch and keygen)
- Python之常用模块(待更新)
- 2016/5/17 初识C++
- 西游记之青鸟大仙
- Elasticsearch中文搜索环境搭建
- 设备文件
- 新建android项目报错,代码中找不到错误
- Codeforces 675c
- Java Set集合知多少?
- 自定义View需要注意的问题
- 谢欣伦 - OpenDev原创教程 - 媒体开发库libMedia
- 家庭仿真之任务场景截取改进
- 解决找不到libvirt/libvirt.h的错误
- linux shell编程学习--循环流程while,for,until命令
- C# 委托(Delegate Action Func Predicate)
- Unable to execute dex: Multiple dex files define 的解决方法
- 内部类
- WEB开发的套路——从MODEL到VIEW