zjuoj 3606 Lazy Salesgirl
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代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
const int maxn = ;
int sum[maxn<<];
double pp[maxn<<][];
struct node{
int id,len,num,tt;
bool operator < (const node &cmp) const{
return tt<cmp.tt;
}
}a[maxn],cnt[maxn];
void update(int pos,int l,int r,int x){
sum[x]++;
if(l==r){
for(int i=;i<;i++) pp[x][i]=1.0*(i+)*a[pos].num;
return ;
}
int m=(l+r)>>;
if(pos<=m) update(pos,lson);
else update(pos,rson);
for(int i=;i<;i++)
pp[x][i]=pp[x<<][i]+pp[x<<|][(sum[x<<]+i)%];
}
int main(){
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i].num);
for(i=;i<=n;i++) scanf("%d",&a[i].tt);
sort(a+,a+n+);
a[].tt=;
for(i=;i<=n;i++) {
cnt[i].tt=a[i].tt-a[i-].tt;
cnt[i].id=i;
}
memset(sum,,sizeof(sum));
memset(pp,,sizeof(pp));
sort(cnt+,cnt+n+);
double aver_ans=,w_ans;
for(i=,j=;i<=n;i=j){
while(cnt[j].tt==cnt[i].tt && j<=n) //j<=n
update(cnt[j].id,,n,);
j++;
}
double tmp=pp[][]*1.0/sum[];
if(tmp>aver_ans){
aver_ans=tmp;
w_ans=cnt[i].tt;
}
}
printf("%.6lf %.6lf\n",w_ans,aver_ans);
}
return ;
}
zjuoj 3606 Lazy Salesgirl的更多相关文章
- ZOJ 3606 Lazy Salesgirl 浙江省第九届省赛
Lazy Salesgirl Time Limit: 5 Seconds Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who ma ...
- ZOJ 3606 Lazy Salesgirl ( 线段树 + 思路 )
卖切糕的小女孩 http://www.cnblogs.com/wuyiqi/archive/2012/04/28/2474672.html #include <cstdio> #inclu ...
- zjuoj 3607 Lazier Salesgirl
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 Lazier Salesgirl Time Limit: 2 Sec ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
- ZOJ 3607 Lazier Salesgirl (枚举)
Lazier Salesgirl Time Limit: 2 Seconds Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes ...
- 代码的坏味道(15)——冗余类(Lazy Class)
坏味道--冗余类(Lazy Class) 特征 理解和维护类总是费时费力的.如果一个类不值得你花费精力,它就应该被删除. 问题原因 也许一个类的初始设计是一个功能完全的类,然而随着代码的变迁,变得没什 ...
- Mach-O 的动态链接(Lazy Bind 机制)
➠更多技术干货请戳:听云博客 动态链接 要解决空间浪费和更新困难这两个问题最简单的方法就是把程序的模块相互分割开来,形成独立的文件,而不再将它们静态的链接在一起.简单地讲,就是不对那些组成程序的目标文 ...
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- Hibernate之lazy延迟加载
一.延迟加载的概念 当Hibernate从数据库中加载某个对象时,不加载关联的对象,而只是生成了代理对象,获取使用session中的load的方法(在没有改变lazy属性为false的情况下)获取到的 ...
随机推荐
- 期望dp BZOJ3450+BZOJ4318
BZOJ3450 概率期望DP f[i]表示到i的期望得分,g[i]表示到i的期望长度. 分三种情况转移: ① s[i]=‘x’:f[i]=f[i-1],g[i]=0 ② s[i]=‘o’:f[i]= ...
- 线上Java应用排查和诊断规范
@郑昀 整理 标准做法一:OOM触发HeadpDump 目的: OOM发生时,输出堆栈快照文件,供研发人员分析. 在JVM中,如果98%的时间是用于 GC 且可用的 Heap size 不足2%的时候 ...
- IE6、7绝对定位层被遮挡的原因(主要是父层决定的)
最近做项目,经常遇到IE7以下浏览器中.一些悬浮框被一些元素遮挡的问题,这些元素一般都是设置了position的.问题的根本在不是被设置绝对定位的元素上,而是在设置了相对定位的父元素上. 我查阅了 ...
- petapoco定制,比较SQL事务,存储过程,分布式事务(MSDTC)的区别和场景
使用分布式事务时 就锁死了,而且是只锁编辑的行 使用.netSQL事务一定要执行了一个CUD的SQL才会锁死,而且也是锁行,但是也锁读的行 .netSQL事务要在这里才锁死 结论,对于产品要求细粒度的 ...
- Android 图片三级缓存
图片缓存的原理 实现图片缓存也不难,需要有相应的cache策略.这里采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cac ...
- Hibernate框架配置
API package com.hanqi.test; import static org.junit.Assert.*; import org.hibernate.SessionFactory; ...
- java: cairo-misc.c:380: _cairo_operator_bounded_by_source: Assertion `NOT_REACHED' failed.
出错原因 该问题会在Centos6.6及更高版本出现.也会在其他版本中出现. 解决方案 禁用carioGraphics > Add -Dorg.eclipse.swt.internal.gtk. ...
- 提交form表单不刷新页面案列
提交form表单不刷新页面其实很简单的,这里拿上传图片来举列,大家有什么其它的方法也欢迎留言告知与我 <form action="" method="post&qu ...
- CSS Bugs 解决方案
说明:本文档兼容性测试基础环境为:windows系统:IE6-IE10, Firefox6.0, Chrome13.0, Safari5.1, Opera11.51 Bugs及解决方案列表(以下实例默 ...
- C# 安装和卸载 Windows Service
特别注意: 安装Window Service 的时候,一定要用管理员打开命令提示符(cmd) 1. 创建Windows Service 服务项目 2. Service设计界面:右键-->选择安装 ...