hdu 1009 FatMouse' Trade (水题,贪心)
2014-07-22 18:09
441 查看
小记:这题比较水
思路:将J[i]/F[i]的值进行从大到小排序,然后依次贪心
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8
const int MAX_ = 1010;
const int N = 100010;
const int INF = 0x7fffffff;
struct node{
int s, e;
double dis;
}t[MAX_];
bool cmp(const node& a, const node& b)
{
if(a.dis < b.dis)return 0;
return 1;
}
int main()
{
int T;
int n, m;
while(scanf("%d%d",&n, &m)) {
if(n == -1 && m == -1)break;
REP(i, 0, m) {
scanf("%d%d", &t[i].s, &t[i].e);
t[i].dis = t[i].s*1.0/t[i].e;
}
sort(t, t+m, cmp);
double ans = 0;
REP(i, 0, m){
if(t[i].e >= n){
ans += t[i].dis * n;
break;
}
else {
ans += t[i].s;
n -= t[i].e;
}
}
printf("%.3f\n", ans);
}
return 0;
}
思路:将J[i]/F[i]的值进行从大到小排序,然后依次贪心
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8
const int MAX_ = 1010;
const int N = 100010;
const int INF = 0x7fffffff;
struct node{
int s, e;
double dis;
}t[MAX_];
bool cmp(const node& a, const node& b)
{
if(a.dis < b.dis)return 0;
return 1;
}
int main()
{
int T;
int n, m;
while(scanf("%d%d",&n, &m)) {
if(n == -1 && m == -1)break;
REP(i, 0, m) {
scanf("%d%d", &t[i].s, &t[i].e);
t[i].dis = t[i].s*1.0/t[i].e;
}
sort(t, t+m, cmp);
double ans = 0;
REP(i, 0, m){
if(t[i].e >= n){
ans += t[i].dis * n;
break;
}
else {
ans += t[i].s;
n -= t[i].e;
}
}
printf("%.3f\n", ans);
}
return 0;
}
相关文章推荐
- hdu 1009 FatMouse' Trade(贪心)
- HDU 1009 FatMouse' Trade(贪心)
- HDU-1009-FatMouse' Trade (贪心,sort,qsort)
- 【HDU-oj]-1009-FatMouse' Trade(贪心)
- 文章标题 HDU 1009 : FatMouse' Trade(贪心)
- HDU 1009 FatMouse' Trade 贪心水题
- HDU 1009 FatMouse' Trade(贪心)
- HDU-1009 FatMouse' Trade 贪心
- hdu 1009 FatMouse' Trade【贪心】
- HDU 1009 - FatMouse' Trade(贪心)
- HDU 1009 FatMouse' Trade (贪心、结构体排序)
- HDU 1009 FatMouse' Trade(贪心)
- hdu1009 FatMouse' Trade(贪心水)
- HDU - 1009 FatMouse' Trade 贪心
- HDU 1009 FatMouse' Trade 贪心菜鸟题
- HDU1009_FatMouse' Trade【贪心】【水题】
- HDU 1009 FatMouse' Trade(水贪心)
- HDU 1009 FatMouse' Trade (贪心)
- hdu 1009 FatMouse' Trade 简单贪心
- HDU(1009)FatMouse' Trade(贪心)