嗜血魂K

导航

1.3 Mixing Milk

直接贪心嗯..这题都会的

/*
ID:y7276571
LANG: C++
TASK: milk
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 5000
typedef struct milk{
int cost, ans;
}milk;
int cmp(const void *a, const void *b)
{
return ((milk*)a)->cost - ((milk*)b)->cost;
}
int main(void)
{
freopen("milk.in", "r", stdin);
freopen("milk.out", "w", stdout);
int need, n, i, cost = 0;
milk mk[MAXN];
memset(mk, 0, sizeof(mk));
scanf("%d%d", &need, &n);
for(i = 0; i < n; i++) scanf("%d%d", &(mk[i].cost), &(mk[i].ans));
qsort(mk, n, sizeof(milk), cmp);
i = 0;
while(need)
{
if(need > mk[i].ans) {need -= mk[i].ans; cost += mk[i].cost*mk[i++].ans; continue; }
cost += need*mk[i].cost; break;
}
printf("%d\n", cost);
return 0;
}



posted on 2011-11-05 23:17  嗜血魂K  阅读(167)  评论(0编辑  收藏  举报