ZOJ 3607 Lazier Salesgirl

Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy that she will fall asleep if no customer comes to buy bread for more than w minutes. When she is sleeping, the customer coming to buy bread will leave immediately. It's known that she starts to sell bread now and the i-th customer come after ti minutes. What is the minimum possible value of w that maximizes the average value of the bread sold?

Input

There are multiple test cases. The first line of input is an integer T ≈ 200 indicating the number of test cases.

The first line of each test case contains an integer 1 ≤ n ≤ 1000 indicating the number of customers. The second line contains n integers 1 ≤ pi ≤ 10000. The third line contains n integers 1 ≤ ti ≤ 100000. The customers are given in the non-decreasing order of ti.

Output

For each test cases, output w and the corresponding average value of sold bread, with six decimal digits.

Sample Input

2
4
1 2 3 4
1 3 6 10
4
4 3 2 1
1 3 6 10

Sample Output

4.000000 2.500000
1.000000 4.000000

贪心用map存储时间间隔。

只有两种情况:

如果后面的时间间隔比前面出现的最大的时间间隔还大的话,新建键值对存到map。

反之,在原来的最大时间间隔上进行处理。

 1 #include <stdio.h>
 2 #include <map>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int n;
 8 int cnt;
 9 int sum[1100];
10 map< double , double > M;
11 map< double , double >::iterator it;
12 
13 struct Node{
14     int p,t;
15 }nod[1100];
16 
17 bool cmp(Node n1, Node n2){
18     return n1.t<n2.t;
19 }
20 
21 int main(){
22     int t;
23     int i,j;
24     scanf("%d",&t);
25     while( t-- ){
26         scanf("%d" ,&n);
27         for(i=0; i<n; i++){
28             scanf("%d" ,&nod[i].p);
29         }
30         for(i=0; i<n; i++){
31             scanf("%d" ,&nod[i].t);
32         }
33         sort(nod ,nod+n ,cmp);
34         M.clear();
35         for(i=0; i<n; i++){
36             if(i==0){
37                 sum[i]=nod[i].p;
38             }else{
39                 sum[i]=sum[i-1]+nod[i].p;
40             }
41         }
42         cnt=nod[0].t;
43         M[cnt]=nod[0].p;
44         for(i=1; i<n; i++){
45             if( nod[i].t-nod[i-1].t>cnt ){
46                 cnt=nod[i].t-nod[i-1].t;
47                 M[cnt]=double(sum[i])/(i+1);
48             }else{
49                 M[cnt]=double(sum[i])/(i+1);
50             }
51         }
52         double w=0;
53         double maxValue=0;
54         for(it=M.begin(); it!=M.end(); it++){
55             if( it->second > maxValue ){
56                 w=it->first;
57                 maxValue=it->second;
58             }
59         }
60         printf("%.6lf %.6lf
",w,maxValue);
61     }
62     return 0;
63 }
原文地址:https://www.cnblogs.com/chenjianxiang/p/3654780.html