ZOJ 3606 Lazy Salesgirl 浙江省第九届省赛

时间:2023-12-05 11:25:56

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 ti minutes. 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 contains n 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

AC代码:线段树
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1e5+;
const int W = ;
int sum[N*];
long long get[W+][N*];
double ansg,anst;
#define left l,m,x<<1
#define right m+1,r,x<<1|1
struct TT
{
int p,t;
bool operator<(const TT &y) const
{
return t<y.t;
} }pre[N];
struct pp
{
int id,w;
bool operator<(const pp &y) const
{
return w<y.w;
}
}mm[N];
void init()
{
ansg = anst = 0.0;
memset(sum,,sizeof(sum));
memset(get,,sizeof(get));
}
void pushup(int x)
{
sum[x] = sum[x<<] +sum[x<< | ];
int tem,i;
for(i=;i<=W;i++)
{
tem = (i+sum[x<<]-)%W+;
get[i][x] = get[i][x<<]+get[tem][x<< | ];
}
}
void update(int pos,long long p,int l,int r,int x)
{
if(l == r)
{
sum[x]++;
for(int i=;i<=W;i++)
get[i][x] = i*p;
return ;
}
int m = (l+r)>>;
if(pos<=m) update(pos,p,left);
if(pos>m) update(pos,p,right);
pushup(x);
}
int main()
{
int T,i,j,n;
double tem;
scanf("%d",&T);
while(T--)
{ init();
scanf("%d",&n);
for( i=;i<n;i++) scanf("%d",&pre[i].p);
for( i=;i<n;i++) scanf("%d",&pre[i].t);
sort(pre,pre+n);//对到来的时间进行排序
mm[].w = pre[].t;
mm[].id = ;
for( i=;i<n;i++)
{
mm[i].w = pre[i].t - pre[i-].t;//取时间差,然后相减,再对时间差进行排序;
mm[i].id = i;
}
sort(mm,mm+n);
for( i=;i<n;)
{
j=i;
while(j<n && mm[i].w == mm[j].w)
{
update(mm[j].id,pre[mm[j].id].p,,n-,);
j++;
}
tem = get[][]*1.0/sum[];
if(ansg <tem)
{
ansg = tem;
anst = mm[i].w;
}
i=j;
}
printf("%.6lf %.6lf\n",anst,ansg);
}
return ;
}