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. java,swift,oc互相转换,html5 web开发跨平台

    java,swift,oc互相转换,html5 web开发跨平台 写一个java->swift的程序,这个程序是做跨平台系统的核心部分swift和oc到java也在考虑之列Swift->J ...

  2. Linux中Postfix邮件接收配置(四)

    Dovecot介绍 MRA邮件取回代理也有很多如courier-imap,cyrus-imap和dovecot这三个个工具,下面重点介绍Dovecot: 1.高安全性.据 Dovecot 的作者声称, ...

  3. Web负载均衡学习笔记之四层和七层负载均衡的区别

    0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...

  4. 20145118 《Java程序设计》课程总结

    20145118 <Java程序设计>课程总结 每周读书笔记连接汇总 假期笔记 http://www.cnblogs.com/cy1123/p/5224305.html 第一周读书笔记 h ...

  5. 20145306 张文锦 网络攻防 web基础

    20145306 网络攻防 web基础 实验内容 WebServer:掌握Apache启停配置修改(如监听端口)前端编程:熟悉HTML+JavaScript.了解表单的概念编写不同的HTML网页,放入 ...

  6. 20145337 《网络对抗技术》Web基础

    20145337 <网络对抗技术>Web基础 一.实验后回答问题 什么是表单? 表单是HTML的一个重要部分,主要用于将用户输入的信息提交到服务器.如果是普通的HTML页面,则当浏览器提出 ...

  7. linux内核分析 1、2章读书笔记

    一.linux历史 20世纪60年代,MIT开发分时操作系统(Compatible TIme-Sharing System),支持30台终端访问主机: 1965年,Bell实验室.MIT.GE(通用电 ...

  8. Asterisk1.8 sip编码协商分析

    在开始分析之前,先对编码协商中可能涉及的asterisk数据结构和变量作些说明.ast_channel:定义一个通用的通道数据结构 struct ast_channel { const struct ...

  9. C++11标准 STL正则表达式 验证电子邮件地址

    转自:http://www.cnblogs.com/yejianfei/archive/2012/10/07/2713715.html 我们最经常遇到的验证,就是电子邮件地址验证.网站上常见.各种网页 ...

  10. luogu P1880石子归并

    石子归并 luogu1880 传送门   noi1995 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得 ...