uva 10026 - Shoemaker's Problem

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

点击打开链接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;
}


目录
相关文章
|
6月前
uva 11991 - Easy Problem from Rujia Liu?
这个题目的意思是输入n个数,m组询问,每组询问包含两个整数k,v,意思是询问整数v第k次出现的位置。
28 0
|
8月前
UVa1531 - Problem Bee
UVa1531 - Problem Bee
34 0
|
8月前
UVa389 - Basically Speaking
UVa389 - Basically Speaking
24 0
|
8月前
uva101 The Blocks Problem
uva101 The Blocks Problem
31 0
|
机器学习/深度学习 自然语言处理
【HDU 5572 An Easy Physics Problem】计算几何基础
2015上海区域赛现场赛第5题。 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意:在平面上,已知圆(O, R),点B、A(均在圆外),向量V。
999 0
|
数据挖掘
poj-1207 THE 3n+1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.
773 0