Star

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 516    Accepted Submission(s): 213

Problem Description
One of Resty's interests is to watch stars. The Stars are so beautiful! Lyra's Vega, Alpha Aquila - Altair and Alpha Cyg consist of the Summer Triangle. Resty likes them very much.
One day, Resty comes to the Moon to have his picnic. Soon he found that he can see so many beautiful stars here! You can never find such a view again - All the beautiful stars are in one line!! So Resty wants to take photos to record the incredible moment.
Resty likes those stars so much so he knows which one is more beautiful. Now he gives each star a score, (a number between 1 and 200000, the higher, the better). So we can use an integer sequence to show the stars from left to right.
Resty's camera is very strange and it will take two photos at one time, and each photo will contain a series of continuous stars in it. No stars will appear in both photos, and even no two stars that adjacent to each other will be in different photos. The number of stars in each photo will between x and y.
Now, Resty tells you the sequence, you must find two photos that the average score of all the stars in the photos is as great as possible.
 
Input
The input consists of more than one test case.
Process to the END OF DATA.
For each test case:
The first line contains 3 integers: n, x, y. n is the number of stars.
1 <= x < y <= n <=50000
The second line contains n integers (between 1 and 200000), the score of each stars.

 
Output
You must output the max average score Resty could get with the precision to 3 digits after the decimal point.
Output Format is "Case ID: ANS" one line for each data
Don't print any empty line to the output
 
Sample Input
5 1 2
1 2 3 4 5
6 2 3
6 1 2 4 3 5
 
Sample Output
Case 1: 4.000
Case 2: 3.800
 
Author
Resty
 
Source
 
     感觉很不错的一道题。
   题意: 从一个序列中找出两个不相邻的连续子序列(且每个子序列的长度必须在[x,y]内),使得两个序列内的数的平均值最大化。
        做法: 二分这个最大值,判定的时候发现这个式子  (s1+s2)/(l1+l2)>=p , s1,s2为两个序列的总和,l1,l2为长度。转移一下发现
就是让序列中的每个数都减去p之后总和仍>=0,想到这就ok了,我们预处理一下数组。然后计算l[i],r[i], l[i]表示在i之前的满足长度条件的序列的最大值,r[i]同理。然后枚举中间点,只要最大值不小于零表示当前的p可行。
  计算l,r时用到了队列优化操作。
   用到了优先队列1s+,但是可以用单调队列优化,明天试试^-^。

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
int a[];
double b[],fl[],fr[];
int n,x,y;
struct node{
int id;
double s;
bool operator< (const node& tmp)const{
return s>tmp.s;
}
};
bool ok(double p){
for(int i=;i<=n;++i){
b[i]=b[i-]+(double)a[i]-p;
}
b[n+]=b[n];
priority_queue<node>q;
fl[]=-inf;
fr[n+]=-inf; for(int i=;i<=n;++i){
if(i-x>=) q.push(node{i-x,b[i-x]});
fl[i]=fl[i-];
while(!q.empty() && i-q.top().id>y) q.pop();
if(!q.empty()){
fl[i]=max(fl[i],b[i]-b[q.top().id]);
}
}
while(!q.empty())q.pop();
for(int i=n;i>=;--i){
if(i+x<=n+) q.push(node{i+x,b[n]-b[i+x-]});
fr[i]=fr[i+];
while(!q.empty() && q.top().id-i>y) q.pop();
if(!q.empty()){
fr[i]=max(fr[i],b[n]-b[i-]-q.top().s);
}
//cout<<"fr="<<fr[i]<<endl;
}
double res=-inf;
for(int i=;i<=n;++i){
res=max(res,fl[i-]+fr[i+]);
}
return res>=;
}
int main(){
int i,j,k,cas=;
while(cin>>n>>x>>y){
for(i=;i<=n;++i) scanf("%d",a+i);
double l=,r=;
while(fabs(l-r)>eps){
double mid=(l+r)/;
if(ok(mid)) l=mid;
else r=mid;
}
printf("Case %d: %.3f\n",++cas,l);
}
return ;
}

hdu-3276-dp+二分+单调队列的更多相关文章

  1. 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...

  2. [P1020]导弹拦截 (贪心/DP/二分/单调队列)

    一道很经典的题 这道题就是要求一个最长单调不升子序列和一个最长单调上升子序列. 先打了一个n2复杂度的 用DP #include<bits/stdc++.h> using namespac ...

  3. [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  4. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  5. NOIP模拟 最佳序列 - 二分 + 单调队列

    题意: 各一个n(\(\le 20000\))的序列,定义纯洁序列为长度len满足\(L \le len \le R\)的序列,纯洁值为某一纯洁序列的平局值,输出所有纯洁序列中最大平均值. 分析: 二 ...

  6. [USACO12MAR]花盆 二分 单调队列

    [USACO12MAR]花盆 二分 单调队列 存在一个长度为\(x\)的区间\([l,r]\),使得区间中最大值与最小值差至少为\(w\),求这个最小的\(x\) \(n\le 100000\),\( ...

  7. 【HDU 3401 Trade】 单调队列优化dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目大意:现在要你去炒股,给你每天的开盘价值,每股买入价值为ap,卖出价值为bp,每天最多买as ...

  8. 2018.09.26洛谷P3957 跳房子(二分+单调队列优化dp)

    传送门 表示去年考普及组的时候失了智,现在看来并不是很难啊. 直接二分答案然后单调队列优化dp检验就行了. 注意入队和出队的条件. 代码: #include<bits/stdc++.h> ...

  9. 洛谷P3975 跳房子 [DP,单调队列优化,二分答案]

    题目传送门 跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一 ...

  10. 【BZOJ2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化DP

    [BZOJ2806][Ctsc2012]Cheat Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的 ...

随机推荐

  1. python3.4学习笔记(二) 类型判断,异常处理,终止程序

    python3.4学习笔记(二) 类型判断,异常处理,终止程序,实例代码: #idle中按F5可以运行代码 #引入外部模块 import xxx #random模块,randint(开始数,结束数) ...

  2. centos 网卡名称修改

    在centos6.4之前,如果6.2,6.3安装后网卡名称都是em开始,如果想用eth0这种名称,或者是自定义名称,可以参照以下来实施. 第一步:修改/boot/grub/grub.conf增加一个 ...

  3. bootstrap table数据分页查询展示

    index.php <html> <head> <link rel="stylesheet" href="./css/bootstrap.m ...

  4. 根据wsdl,基于wsimport生成代码的客户端

    根据wsdl,基于wsimport生成代码的客户端 wsimport是jdk自带的命令,可以根据wsdl文档生成客户端中间代码,基于生成的代码编写客户端,可以省很多麻烦. 局限性:wsimport   ...

  5. curl 7.52.1 for Windows

    curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本. 这个工具对于在运维.持续集成和批处理场 ...

  6. 20145301 赵嘉鑫 《网络对抗》Exp6 信息搜集与漏洞扫描

    20145301赵嘉鑫<网络对抗>Exp6 信息搜集与漏洞扫描 基础问题回答 哪些组织负责DNS,IP的管理? 全球根服务器均由美国政府授权的ICANN统一管理,负责全球的域名根服务器.D ...

  7. MIME协议(详解范例)

    转载一:http://blog.csdn.net/bripengandre/article/details/2192982 转载二:http://blog.csdn.net/flfna/article ...

  8. Visio 画图去掉页边距(图形四周的空白区域)的解决办法

    步骤如下: 1.打开Visio对象后在[文件]菜单[选项]菜单项上单击,然后单击[自定义功能区]. 2.在[自定义功能区]的主选项卡中找到[开发工具]复选框,打上勾,单击确定. 3.在Visio对象的 ...

  9. dubbo 实战总结

    1,出现重复调用.因为有重试机制,可以改为异步调用或者幂等操作.

  10. 2018 Machine Learning

    2018/8/13 线性模型(西瓜书P53~P73) Optimizer https://blog.csdn.net/u012151283/article/details/78154917 2018/ ...