Lazier Salesgirl
ZOJ - 3607Kochiya 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.
OutputFor each test cases, output w and the corresponding average value of sold bread, with six decimal digits.
Sample Input2 4 1 2 3 4 1 3 6 10 4 4 3 2 1 1 3 6 10Sample Output
4.000000 2.500000 1.000000 4.000000
题意:
一个小姑娘非常懒但是卖面包非常厉害。她能在以Pi的价格将面包卖给第i个人。但是她一旦在w的时间段内木有顾客,她就会睡着,一觉不起。。。
现在给出买个每个顾客的价钱和相关时间点,现在想要求出在最少的w里卖出最大的面包价钱的平均值,问这一平均值和w是多少。
思路:重点就她只要是睡着了就会一觉不起,后面的都不能买了,所以当前对象的w是之前所有时间间隔中最大的,不然就卖不到这了,也就是每次取w[i] = max(t[i]-t[i-1],w[i-1]),因为假设后面出现了一个间隔小的,比之前的小,那说明从一开始她就没等到顾客来就睡着了,然后后面的就都不可能卖出去了,所以只能不断取最大,然后遍历每个w[i],只要这个间隔大于等于其他两个顾客到来的时间间隔说明她不会睡着会等到顾客来,就加上这个值记下数量,如果小于了,说明等不到了睡着了,之后的都卖不出了,就break,然后比较取平均值最大;
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000+100;
int p[maxn],t[maxn],w[maxn];
int T,n;
int main(){
cin >> T;
while(T--){
cin >> n;
for(int i = 0; i < n; i++){
cin >> p[i];
}
for(int i = 0; i < n; i++){
cin >> t[i];
}
w[0] = t[0];
for(int i = 1; i < n; i++){
w[i] = max(t[i]-t[i-1],w[i-1]);
}//这里必须不断取最大的,如果有个小于了之前的,那么遍历这个的时候,从一开始就等不到顾客来,就睡了,后面的都买不了了
double minw,sum,ave,time;
ave = 0;
for(int i = 0; i < n; i++){
time = w[i];
sum = 0;
int j;
for(j = 0; j < n; j++){
if(time >= w[j])
sum += p[j];
else
break;//只要小于了,说明等不到了睡着了直接退出
}
if(ave < sum / j){
ave = sum / j;
minw = time;
}
}
printf("%.6f %.6f\n",minw,ave);
}
return 0;
}