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. oracle_存储过程小记

    # 刷新会员标签函数 {color:red} fun_refresh_code{color} {noformat}CREATE OR REPLACE FUNCTION fun_refresh_code ...

  2. P2864 [USACO06JAN]树林The Grove

    P2864 [USACO06JAN]树林The Grove 神奇的射线法+bfs 裸的bfs很难写....... 那么我们找一个最外围障碍点,向图的外边引一条虚拟射线. 蓝后bfs时经过这条射线奇数次 ...

  3. Docker搭建RabbitMQ集群

    Docker搭建RabbitMQ集群 Docker安装 见官网 RabbitMQ镜像下载及配置 见此博文 集群搭建 首先,我们需要启动运行RabbitMQ docker run -d --hostna ...

  4. HTML JavaScript 基础学习

    HTML 中肯定会用到 JavaScript 的知识点,会点 JavaScript 的基础知识不会吃亏,其实打算去买JavaScript的教程去专门学习一下,但是交给我的时间不多了,记录一点,能会一点 ...

  5. switch to hexo !

    用 hexo 搭了个博客 但是由于本博客内容过多,所以对于少数内容搬迁,而且本博客和那个博客相关联,但是这个博客不再更新. 新站地址 再见!

  6. duilib中edit获得鼠标焦点后右边框被覆盖

    转载:http://www.cnblogs.com/minggong/p/6457734.html 用duilib做了一个窗口,窗口内有一个供用户输入使用的是edit控件. XML中是这样写的: &l ...

  7. Timer,TimerTask,Handler

    新建一个定时器线程,通过此线程每一秒发送数据到Handler,然后通过Handler来修改UI. 1.获得Handler,Timer,TimerTask对象. Handler handler=new ...

  8. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  9. Chromosome coordinate systems: 0-based, 1-based

    From: https://arnaudceol.wordpress.com/2014/09/18/chromosome-coordinate-systems-0-based-1-based/ I’v ...

  10. php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式

    做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...