zoj 3607 Lazier Salesgirl 暴力 前缀和

来源:互联网 发布:知乎提问后自己找不到 编辑:程序博客网 时间:2024/06/02 14:06

Lazier Salesgirl

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 containsn 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

241 2 3 41 3 6 1044 3 2 11 3 6 10

Sample Output

4.000000 2.5000001.000000 4.000000

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607


题意:

某 在0时刻开店, 然后按顺序来了n个客人,分别出了一个价格p[i],每个人来的时刻为 t[i]。

然后 某  在w时间内没接待客人就会睡觉,如果间隔正好是w,不会睡。 问接待的客人 出价的平均值最大是多少,在满足该条件下 w最低是多少。


做法:

不断求前缀和  还有 到第i个人间隔最大值。 接待的人肯定是连续的 前多少个。   先假设只接待第一个人,然后假设接待前两个,再前三个。 类推。 

如果是正好前i个人,那么w得  大于等于 ( 到第i个人间隔最大值) ,而且要严格小于(第 i和i+1 个人的间隔差)。 所以w就 取 ( 到第i个人间隔最大值),

如果这个值严格小于 (第 i和i+1 个人的间隔差)那么就成立,判断前i个人的平均价格是否更低。


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <math.h>#include <string>#include <iostream>#include <algorithm>using namespace std;#include <stack>#include <queue>#include <vector>#include <deque>#include <set>#include <map>#define INF 999999999#define eps 0.00001#define LL __int64d#define pi acos(-1.0)struct node{int p, t;int sum;int big;//qian cha zui xiaoint cha;}a[1047];int cmp(node a,node b){return a.sum>b.sum;}int main(){int t;scanf("%d",&t);//xiao w  da pingjunwhile(t--){int n, i, j;double sum = 0;scanf("%d",&n); a[0].sum=0;a[0].t=0;for(i = 1; i <= n; i++){scanf("%d",&a[i].p); a[i].sum=a[i-1].sum+a[i].p;}for(i = 1; i <= n; i++){scanf("%d",&a[i].t); if(i==1)a[i].big=a[i].t-a[i-1].t;elsea[i].big=max(a[i-1].big,a[i].t-a[i-1].t);}for(i=1;i<=n;i++){if(i==n)a[i].cha=99999999;elsea[i].cha=a[i+1].t-a[i].t;}//sort(a+1,a+1+n,cmp);double bigavg=-1;double big;for(i=1;i<=n;i++){if((1.0*a[i].sum/i)>bigavg){if(a[i].cha>a[i].big){big=a[i].big;bigavg=(1.0*a[i].sum/i);} } }printf("%.6lf %.6lf\n",big,bigavg);}return  0;}




0 0
原创粉丝点击