您的位置:首页 > 其它

HDU 1009 FatMouse' Trade

2014-08-20 16:05 274 查看
忽然发现HDU上还有一道残留的没A过去的水题,那是当年年轻的我太菜了(现在依然很菜)。

简单的贪心,因为老鼠可以用猫粮等比例的换取每个房间里的Java豆,所以我们先换汇率高的,再换汇率低的。

//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1000 + 10;
struct Trade
{
double f, j;
}trade[maxn];

bool cmp(Trade a, Trade b)
{
return (a.j*b.f > a.f*b.j);
}

double ans, tot;
int n;

int main(void)
{
#ifdef LOCAL
freopen("1009in.txt", "r", stdin);
#endif

while(scanf("%lf%d", &tot, &n) == 2)
{
if(n == -1)    break;
for(int i = 0; i < n; ++i)
scanf("%lf%lf", &trade[i].j, &trade[i].f);
sort(trade, trade + n, cmp);
int i = 0;
ans = 0;
while(tot > 0 && i < n)
{
if(tot >= trade[i].f)
{
ans += trade[i].j;
tot -= trade[i++].f;
}
else
{
ans += tot * trade[i].j / trade[i].f;
tot = 0;
}
}
printf("%.3lf\n", ans);
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: