Shoemaker’s problem(贪心)

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each ithjob, it is known the integer Ti(1<=Ti<=1000), the time in days it takes the shoemaker to finish the job. For each day of delay before starting to work for the ithjob, shoemaker must pay a fine of Si(1<=Si<=10000) cents. Your task is to help the shoemaker, writing a programm to find the sequence of jobs with minimal total fine.

The InputThe input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

First line of input contains an integer N (1<=N<=1000). The next N lines each contain two numbers: the time and fine of each task in order.

The OutputFor each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

You programm should print the sequence of jobs with minimal fine. Each job should be represented by its number in input. All integers should be placed on only one output line and separated by one space. If multiple solutions are possible, print the first lexicographically.

Sample Input143 41 10002 25 5Sample Output2 1 3 4

题意:一个鞋匠接到很多订单。但是,每个客户都认为自己的订单应该被马上处理。因此,对于第i个订单,,在开始处理这个订单之前,每天都要付罚金Si (1<=Si≤<=1000)。而他一天只能处理一个订单,而且一个订单可能需要很多天才能完成。对于第i个订单,整数Ti (1<=Ti<=1000)代表处理完成这个订单所需要的天数。求所付罚金最少的订单处理顺序

思路:因为鞋匠时同时接到所有订单,起始罚款的日期相同,贪心fine/day ,就是鞋匠一天可以减少的最大损失,稳定排序即可。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;struct node{int id;double t,f;}q[1010];int cmp(struct node a,struct node b){if(fabs((a.f/a.t)-(b.f/b.t)>0.00001))return (a.f/a.t)>(b.f/b.t);return a.id<b.id;}int main(){int T,n,i;scanf("%d",&T);while(T–){scanf("%d",&n);for(i=0;i<n;i++){scanf("%lf %lf",&q[i].t,&q[i].f);q[i].id=i+1;}sort(q,q+n,cmp);for(i=0;i<n-1;i++)printf("%d ",q[i].id);printf("%d\n",q[i].id);if(T)printf("\n");}return 0;}

也许不是自己该去发挥的地方,还是让自己到最适合自己战斗的方面去吧!勇敢的接受自己的失败,

Shoemaker’s problem(贪心)

相关文章:

你感兴趣的文章:

标签云: