#include"stdlib.h"
#include <algorithm>
#include <vector>
#include<math.h>
#include<time.h>
#include <iostream>
#include <fstream>
#include<ctime>
#include <map>
#include <algorithm>
using namespace std;
bool cmp(const pair<int, int>& x, const pair<int, int>& y)//false:调换位置
{
if (y.second > x.second)
return true;
if (y.second < x.second)
return false;
return false; //Should return false if both the vaules are same
/* if (x.second > y.second)
return false;
return true;*/
}
int main(int argc, char** argv) {
freopen("5.txt", "r", stdin);
int n, r, avg;
cin >> n >> r >> avg;
vector<pair<int, int> > vmap;
for (int i = 0;i<n; i++)
{
int a, b;
cin >> a >> b;
vmap.push_back(make_pair(a, b));
}
sort(vmap.begin(), vmap.end(), cmp);
int fuyu = 0;
int jige = 0;
for (vector<pair<int, int> >::iterator it = vmap.begin(); it != vmap.end(); it++)
{
if (it->first > avg)//每门超过平均分可分配的分数;
fuyu += it->first - avg;
if (it->first < avg)
jige += avg - it->first;//每门达到平均分所需要的分数;
}
int output = 0;
if (fuyu >= jige)
cout << 0;
else
{
int needscore = jige - fuyu;
for (vector<pair<int, int> >::iterator it = vmap.begin(); it != vmap.end(); it++)
{
if (needscore == 0)break;
if (needscore > (r - it->first))
{
output += (r - it->first)*it->second;
needscore -= r - it->first;
}
else
{
output += needscore*it->second;
needscore = 0;
}
}
cout << output;
}
return 0;
}
奖学金-贪心算法
最新推荐文章于 2023-11-24 02:30:00 发布