Lazier Salesgirl ZOJ - 3607

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

题意: T种情况,每种n个客人,第i个客人可赚p[i]元钱,第i个客人t[i]时间来,卖东西的很懒,如果w时间内没人来就睡觉,一觉不起,求最大人均赚的钱同时输出最小w。

暴力枚举每次在第i个顾客来后睡。用w[i]保存想赚第i个人w的最小值,用av[i]保存赚前i个顾客的平均值。如果在第i个顾客来后睡觉就要满足w[i]<w[i+1](这个式子表明第i和第i+1个顾客之间的时间间隔比之前的都大,所以可以取w=w[i]来在这点后睡觉不醒)


#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<cstring>
#include<set>
#include<map>
using namespace std;
const int maxn = 1002;
int T, n;
int p[maxn], t[maxn];
double w[maxn], ave[maxn];
double sum;
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        memset(p, 0, sizeof(p));
        memset(t, 0, sizeof(t));
        memset(w, 0, sizeof(w));
        memset(ave, 0, sizeof(ave));
        sum = 0;
        scanf("%d", &n);

        //输入每个人对应的钱数,并且计算前i个人的平均值;
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &p[i]);
            sum+=p[i];
            ave[i] = sum/i;
        }

        double wmax = -1;

        for(int i=1; i<=n; i++)
        {
            scanf("%d", &t[i]);

            if(i!=1)
            {
                //更新前i个间隔的最大间隔;即第i个人与第i-1个人的间隔如果大于前面所有的间隔,标记这个间隔为前i个间隔的最大值,否则,最大值不变;
                if(t[i]-t[i-1]>wmax)
                {
                    wmax = t[i]-t[i-1];
                    w[i] = wmax;
                }
                else
                    w[i] = wmax;
            }
            else//第一个顾客前的间隔:特殊位置;
            {
                wmax = t[1];
                w[i] = wmax;
            }
        }
        w[n+1] = 100000000000;
        int ans = 0;
        double avemax = -1;
        for(int i=1; i<=n+1; i++)
        {
            //如果是正好前i个人,那么w得大于等于(到第i个人间隔最大值), 而且要严格小于第i和i+1 个人的间隔差。所以w就取到第i个人间隔最大值;(因为p大于1,如果不严格小于,w在不变的情况下是可以取到第i+1个p的,平均值更大)
            if(w[i]<w[i+1])
            {
                //遍历最大的平均值;
                if(ave[i]>avemax)
                {
                    avemax = ave[i];
                    ans = i;
                }
            }
        }
        printf("%.6lf %.6lf\n", w[ans], ave[ans]);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值