ZOJ3606-Lazy Salesgirl

Lazy Salesgirl

Time Limit: 5 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 some pieces of bread at price pi for each piece. 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 wake her up and leave without buying anything. Once she is woken up, she will start to sell bread again until she encounters another gap of w minutes. What's more weird, she can sell 1 + ((k - 1) mod 3) pieces of bread when she sells at the k-th time. It's known that she starts to sell bread now and the i-th customer comes after timinutes. What is the minimum possible value of w that maximizes the average value of the bread sold each time?

Input

There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.

The first line of each test case contains an integer 1 ≤ n ≤ 105 indicating the number of customers. The second line contains n integers 1 ≤ pi ≤ 106. The third line containsn integers 1 ≤ ti ≤ 107. All ti are different.

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
1 2 3 4
4 7 9 10
Sample Output
3.000000 4.666667
3.000000 6.666667

Author:  WU, Zejun
Contest:  The 9th Zhejiang Provincial Collegiate Programming Contest



题意:有一个售货员,n个顾客,卖给 i这个顾客的价格是pi,i 顾客来买东西的时刻为ti,如果有w的时间段没人来买东西,售货员就会睡着,下一个顾客来时会叫醒她,但是不买东西。售货员会卖给第i个来(相对顺序)的顾客1+((i-1)%3)的数量的面包,问使得平均的销售额最大的最小w以及此时的平均销售额是多少( 总的销售额/顾客的个数)

解题思路:线段树,每个节点记录下这个区间最左边的人分别买1,2,3个面包的销售总额和这个区间内有多少个顾客


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int sum[400009],n;
LL x[400009][5];
struct person
{
    int p,t;
    bool operator<(const person &a)const
    {
        return t<a.t;
    }
}a[100005];

struct node
{
    int w,id;
    bool operator <(const node &a)const
    {
        return w<a.w;
    }
}b[100005];

void Merge(int k,int l,int r)
{
    for(int i=1;i<=3;i++)
    {
        int p=(i+sum[l]-1)%3+1;
        x[k][i]=x[l][i]+x[r][p];
    }
}

void update(int k,int l,int r,int pos,int val)
{
    sum[k]++;
    if(l==r)
    {
        for(int i=1;i<=3;i++)
            x[k][i]=i*val;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) update(k<<1,l,mid,pos,val);
    else update(k<<1|1,mid+1,r,pos,val);
    Merge(k,k<<1,k<<1|1);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i].p);
        for(int i=1;i<=n;i++) scanf("%d",&a[i].t);
        sort(a+1,a+1+n);
        a[0].t=0;
        for(int i=1;i<=n;i++)
        {
            b[i].w=a[i].t-a[i-1].t;
            b[i].id=i;
        }
        sort(b+1,b+1+n);
        memset(sum,0,sizeof sum);
        memset(x,0,sizeof x);
        double ma=0,ans=0;
        for(int i=1;i<=n;)
        {
            int j=i;
            while(j<=n&&b[i].w==b[j].w)
            {
                update(1,1,n,b[j].id,a[b[j].id].p);
                j++;
            }
            if(ma<1.0*x[1][1]/sum[1])
            {
                ma=1.0*x[1][1]/sum[1];
                ans=1.0*b[i].w;
            }
            i=j;
        }
        printf("%.6lf %.6lf\n",ans,ma);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值