奖学金-贪心算法

#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;
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值