您的位置:首页 > 其它

uva 10026 - Shoemaker's Problem

2012-08-19 10:38 369 查看
点击打开链接uva 10026

题目意思:    有一个人现在要去做N个任务,每一个任务对应一个完成的时间T,和这个任务开始之前每一天必须要罚的前fine,要求找到一个完成任务的顺序使得,这个总的Fine值最小,输出这个顺序

解题思路:     1:贪心:对输入的Time和Fine,做Fine/Time比值,然后对每一个任务进行排序,这样就能够保证先做那些比例高的任务,这样就能够保证最小的Fine值

                       2:注意自定义cmp函数的运用,以及double类型的比较

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
#define MAXN 1010

int T , n;
int num[MAXN] , t[MAXN] , f[MAXN];
//自定义cmp
bool cmp(int a , int b){//传入两个任务编号
double s1 , s2;
s1 = (f[a-1]*1.0)/t[a-1];
s2 = (f[b-1]*1.0)/t[b-1];
if(s1-s2 > 1e-9) return true;//注意高精度的比较
return false;
}

void solve(){
sort(num , num+n , cmp);
printf("%d" , num[0]);
for(int i = 1 ; i < n ; i++)
printf(" %d" , num[i]);
printf("\n");
}

int main(){
//freopen("input.txt" , "r" , stdin);
scanf("%d%*c" , &T);
while(T--){
scanf("%d" , &n);
for(int i = 0 ; i < n ; i++){
scanf("%d%d" , &t[i] , &f[i]);
num[i] = i+1;
}
solve() ; if(T) printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c