BOJ 1576 Job Scheduling

Job Scheduling

Accept:13     Submit:24

Time Limit:1000MS     Memory Limit:65536KB

        

Job Scheduling

Description

We are given independent and indivisible jobs numbered from 1 to N . They should be executed sequentially in any order. The later the execution of a job starts the longer it lasts - precisely, the time of execution of the job i is hi(t)=ai∗t+bi, if we start it in the moment t.

The goal is to schedule the jobs so that the total execution time is the shortest.

Input Format

The first line is the case number T(1≤T≤2000).For each case the first line is the job number N(1≤N≤20).In each of the following N lines there is a pair of nonnegative real numbers.

Output Format

Output the minimal time needed to finish all jobs,in accuracy of two decimal places.

Sample Input

1

5

0.002000 0.003000

0.016000 0.001000

0.100000 0.300000

0.016000 0.005000

0.030000 0.060000

Sample Output

0.38

 

新生排位赛第三场,打得相当的差,一道题都没AC。

这道题是第三场的第一道题,听学长讲完,感觉做不出来真是不应该

题中给出每个任务执行的时间为ai*t+bi,那么给出两个任务(a1,b1),(a2,b2)

      先执行任务1再执行任务2所用时间为:a1*t+b1+a2*(a1*t+b1)+b2=a1*t+b1+a2*a1*t+a2*b1+b2

      先执行任务2再执行任务1所用时间为:a2*t+b2+a1*(a2*t+b2)+b1=a2*t+b2+a1*a2*t+a1*b2+b1

      比较两种顺序的结果可以发现,两者相差的只有a2*b1和a1*b2

因此只要根据这个结论,按次规则将给定的输入从小到大排序,就能顺利求出结果(我的代码中用的是快排)。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 typedef struct data
 7 {
 8     double a;
 9     double b;
10 } DATA;
11 
12 DATA h[21];
13 
14 int Partition(int p,int r)
15 {
16     DATA x=h[r];
17     int i=p-1;
18 
19     for(int j=p;j<r;j++)
20     {
21         if(h[j].b*x.a<h[j].a*x.b)
22         {
23             DATA temp=h[j];
24             h[j]=h[i+1];
25             h[i+1]=temp;
26             i++;
27         }
28     }
29 
30     DATA temp=h[r];
31     h[r]=h[i+1];
32     h[i+1]=temp;
33 
34     return i+1;
35 }
36 
37 void QuickSort(int p,int r)
38 {
39     if(p<r)
40     {
41         int q=Partition(p,r);
42         QuickSort(p,q-1);
43         QuickSort(q+1,r);
44     }
45 }
46 
47 
48 
49 int main()
50 {
51     int n,t;
52 
53     scanf("%d",&t);
54 
55     while(t--)
56     {
57         scanf("%d",&n);
58 
59         for(int i=1;i<=n;i++)
60             scanf("%lf %lf",&h[i].a,&h[i].b);
61 
62         QuickSort(1,n);
63 
64         double result=0;
65 
66         for(int i=1;i<=n;i++)
67             result+=result*h[i].a+h[i].b;
68 
69         printf("%.2f\n",result);
70     }
71 
72     return 0;
73 }
[C++]

 

posted @ 2013-07-11 23:23  ~~Snail~~  阅读(277)  评论(0编辑  收藏  举报