HDU1009—贪心

题目的链接吃的有点饱,不是太想读题,大概的意思就是:一只fatmouse和一些cat交易bulabula的一堆1,其实就是买东西怎么买划算,这很显然啊,当然是单价越低的越划算喽,所以排序的规则就出来了。然后贪心的选取就好了。比较水就不再继续继续分析了,直接写代码。

#include"pch.h"
#include<iostream>
#include<algorithm>
#include<vector>
#pragma warning(disable:4996)
const int maxn = 1e6 + 10;
using namespace std;
struct food
{
	int j, f;
}F[maxn];
bool cmpj(food&x, food&y)
{
	return x.j*1.0/x.f > y.j*1.0/y.f;
}
int main()
{
	int M, N;
	food t;
	while (scanf("%d%d", &M, &N))
	{
		double ans = 0;
		if (M == -1 && N == -1)
		{
			break;
		}
		else
		{
			for (int i = 0; i < N; i++)
			{
				scanf("%d%d", &F[i].j, &F[i].f);
			}
			sort(F, F + N, cmpj);
			for (int i = 0; i < N; i++)
			{
				if (M >= F[i].f)
				{
					M -= F[i].f;
					ans += F[i].j;
				}
				else
				{
					double tem =M * 1.0/F[i].f;
					ans += F[i].j*tem;
					M = 0;
					break;
				}
			}
		}
		printf("%0.3lf\n", ans);
	}

	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值