码迷,mamicode.com
首页 > 其他好文 > 详细

zjuoj 3606 Lazy Salesgirl

时间:2015-05-02 23:21:47      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3606

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

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

 

 

分析:

有一个售货员,n个顾客,卖给 i 这个顾客的价格是pi,i 顾客来买东西的时刻为 ti

如果有w的时间段没人来买东西,售货员就会睡着,下一个顾客来时会叫醒她,但是不买东西

售货员会卖给第i个来(相对顺序)的顾客 1 + ((i - 1) mod 3)的数量的面包 即 1 2 3 中的一个

问使得平均的销售额最大的最小w以及此时的平均销售额是多少( 总的销售额/顾客的个数)

 

做法:我觉得关键是要抓住题目的特点,深挖下去,售货员每隔w时间会睡着,那就意味着,

1:如果有个顾客和上一个顾客间的时间间隔超过了w,这个顾客就不糊买东西,而与上一个顾客来的时间间隔小于等于w的顾客肯定能买到面包

所以我们只需枚举每个时间间隔既可,答案肯定就是某个时间间隔,一旦时间间隔定了,那能卖的面包数以及买到东西的顾客数也就定了

2:买到面包的数量问题,由题目给出的式子可得,卖出面包数量的序列 为1 2 3 1 2 3.。。。所以如果知道了某个人买了几个面包,那么可以

推算出接下来第x个人买了几个面包,因为最多也只有三种情况,所以把三种情况的结果都保存一下,就用到了三个线段树,其实就是线段树的域开个二维数组

线段树的域:

sum[rt]:记录当前节点的区间内共有几个顾客

pp[rt][3]:记录当区间最左边的人分别买了 1 2 3个面包时总的销售额

那么我们可以通过线段树将这个结果传递上去

关键代码:

for(int i=0;i<3;i++)
        pp[x][i]=pp[x<<1][i]+pp[x<<1|1][(sum[x<<1]+i)%3];

知道了左子树第一个人买的面包个数,和左子树的人数,自然就可以推出右子树第一个人买的面包个数,三种情况都记录一下,然后再传递上去

最后的结果是pp[1][0],因为第一个人肯定只买了一个面包;

 

 

AC代码:

技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 #define lson l,m,x<<1
 6 #define rson m+1,r,x<<1|1
 7 const int maxn = 100010;
 8 int sum[maxn<<2];
 9 double pp[maxn<<2][3];
10 struct node{
11     int id,len,num,tt;
12     bool operator < (const node &cmp) const{
13         return tt<cmp.tt;
14     }
15 }a[maxn],cnt[maxn];
16 void update(int pos,int l,int r,int x){
17     sum[x]++;
18     if(l==r){
19         for(int i=0;i<3;i++)  pp[x][i]=1.0*(i+1)*a[pos].num;
20         return ;
21     }
22     int m=(l+r)>>1;
23     if(pos<=m) update(pos,lson);
24     else update(pos,rson);
25     for(int i=0;i<3;i++)
26         pp[x][i]=pp[x<<1][i]+pp[x<<1|1][(sum[x<<1]+i)%3];
27 }
28 int main(){
29     int t,n,i,j;
30     scanf("%d",&t);
31     while(t--){
32         scanf("%d",&n);
33         for(i=1;i<=n;i++)  scanf("%d",&a[i].num);
34         for(i=1;i<=n;i++)  scanf("%d",&a[i].tt);
35         sort(a+1,a+n+1);
36         a[0].tt=0;
37         for(i=1;i<=n;i++) {
38             cnt[i].tt=a[i].tt-a[i-1].tt;
39             cnt[i].id=i;
40         }
41         memset(sum,0,sizeof(sum));
42         memset(pp,0,sizeof(pp));
43         sort(cnt+1,cnt+n+1);
44         double aver_ans=0,w_ans;
45         for(i=1,j=1;i<=n;i=j){
46             while(cnt[j].tt==cnt[i].tt && j<=n) //j<=n 
47                 update(cnt[j].id,1,n,1);
48                 j++;
49             }
50             double tmp=pp[1][0]*1.0/sum[1];
51             if(tmp>aver_ans){
52                 aver_ans=tmp;
53                 w_ans=cnt[i].tt;
54             }
55         } 
56         printf("%.6lf %.6lf\n",w_ans,aver_ans);
57     }
58     return 0;
59 }
View Code

 

zjuoj 3606 Lazy Salesgirl

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4472594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!