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
题意:在ti分钟会有第i个客人过来,卖给他面包可以赚pi,但一旦卖给上一个客人后超过w分钟以后,店主就不会再醒来。要求的是题意最后那句话,求最多平均盈利下最短间隔时间
平均盈利=盈利总和 / i个卖出面包的顾客
思路:不难看出,如果到第i个顾客平均盈利最多,那么它的间隔时间一定是这个顾客以前间隔是间最长的,即tt=max(tt,t[i]-t[i-1]),这个会在找到最大平均盈利时进行更新。但很容易忽略一点,当找到最大盈利时,你所求的间隔时间,还可以使下一个顾客来的时候没在睡觉的状态,即t[i+1]-t[i]>tt 。 第一次,没考虑到那一点,写了注释那里面的代码,后来改成两个//,之间的代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int p[1005],t[1005];
int main(void){
int T;
int n;
double ans1,ans2;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&p[i]);
for(int i=0;i<n;i++)
scanf("%d",&t[i]);
ans1=0,ans2=0;
double temp=0,sum=0,tt=0;
for(int i=0;i<n;i++){
sum+=p[i];
tt=max(tt,t[i]-temp);
/*
if(sum/(i+1)>ans2){
ans2=sum/(i+1);
ans1=tt;
}
temp=t[i];
*/
//
if(i==n-1){
if(sum/(i+1)>ans2){
ans2=sum/(i+1);
ans1=tt;
}
}
else{
if(tt<t[i+1]-t[i]&&sum/(i+1)>ans2){ //下一个顾客来时超过这个间隔,即下一个顾客来时已经睡了
ans2=sum/(i+1);
ans1=tt;
}
}
//
temp=t[i];
}
printf("%.6f %.6f\n",ans1,ans2);
}
return 0;
}