您的位置:首页 > 其它

ZOJ 3607 Lazier Salesgirl

2014-04-09 17:59 357 查看
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。

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

#include <stdio.h>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int cnt;
int sum[1100];
map< double , double > M;
map< double , double >::iterator it;

struct Node{
int p,t;
}nod[1100];

bool cmp(Node n1, Node n2){
return n1.t<n2.t;
}

int main(){
int t;
int i,j;
scanf("%d",&t);
while( t-- ){
scanf("%d" ,&n);
for(i=0; i<n; i++){
scanf("%d" ,&nod[i].p);
}
for(i=0; i<n; i++){
scanf("%d" ,&nod[i].t);
}
sort(nod ,nod+n ,cmp);
M.clear();
for(i=0; i<n; i++){
if(i==0){
sum[i]=nod[i].p;
}else{
sum[i]=sum[i-1]+nod[i].p;
}
}
cnt=nod[0].t;
M[cnt]=nod[0].p;
for(i=1; i<n; i++){
if( nod[i].t-nod[i-1].t>cnt ){
cnt=nod[i].t-nod[i-1].t;
M[cnt]=double(sum[i])/(i+1);
}else{
M[cnt]=double(sum[i])/(i+1);
}
}
double w=0;
double maxValue=0;
for(it=M.begin(); it!=M.end(); it++){
if( it->second > maxValue ){
w=it->first;
maxValue=it->second;
}
}
printf("%.6lf %.6lf\n",w,maxValue);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: