Problem Description
PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order
to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies
that:



 
Input
The input consists of multiple test cases. The number of test cases T(T<=100) occurs in the first line of input.



For each test case:

The first line contains a single integer N (1<=N<=100000), which denotes the length of A and B.

The second line consists of N integers, where the ith denotes Ai.
 
Output
Output the minimal f (A, B) when B is optimal and round it to 6 decimals.
 
Sample Input
4
9
1 1 1 1 1 0 0 1 1
9
1 1 0 0 1 1 1 1 1
4
0 0 1 1
4
0 1 1 1
 
Sample Output
1.428571
1.000000
0.000000
0.000000

能够分析出 所求的区间 也就是 从第一个为1開始的到最后一个0结束。每段都是形如111...111000...000这样先为1后为0 的小区间里 B值都是水平的。那么先求出当前一零区间的最优值,假设当前的高度最优值大于单调增栈里 栈首元素的高度,那么能够直接入栈,假设小于,就取出栈首元素与当前区间进行合并,再次入栈。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int mod = 1000000007;
int t, n, a[maxn];
struct C {
int num1, num0;
double res, h;
};
double Cu(double x, double y) {
return x*y/(x+y);
}
double Ch(double x, double y) {
return x/(x+y);
}
int main()
{
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
int st = 1, en = n, i, j, k;
for(i = 1; i <= n; i++) scanf("%d", &a[i]);
for(i = 1; i <= n && a[i] == 0; i++) ; st = i;
for(i = n; i >= 1 && a[i] == 1; i--) ; en = i;
stack <C> zhan;
for(i = st; i <= en; ) {
int u = 0, d = 0;
for(j = i; j <= en; j++) {
if(a[j] == 0) break;
u++;
}
for(k = j; k <= en; k++) {
if(a[k] == 1) break;
d++;
}
C aa;
aa.num0 = d;
aa.num1 = u;
aa.res = Cu(u, d);
aa.h = Ch(u, d);
while(!zhan.empty() && zhan.top().h > aa.h) {
C tmp = zhan.top();
zhan.pop();
aa.num1 = tmp.num1 + aa.num1;
aa.num0 = tmp.num0 + aa.num0;
aa.res = Cu(aa.num1, aa.num0);
aa.h = Ch(aa.num1, aa.num0);
}
zhan.push(aa);
i = k;
}
double sum = 0;
while(!zhan.empty()) {
C tmp = zhan.top();
zhan.pop();
sum += tmp.res;
}
printf("%.6lf\n", sum);
}
return 0;
}



HDU 4923 Room and Moor (多校第六场C题) 单调栈的更多相关文章

  1. 2014 HDU多校弟六场J题 【模拟斗地主】

    这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...

  2. Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...

  3. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...

  4. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  5. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  6. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  7. HDU 4923 Room and Moor(推理+栈维护)

    HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...

  8. HDU 4923 Room and Moor (单调栈)

    题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...

  9. 【HDU】4923 Room and Moor(2014多校第六场1003)

    Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

随机推荐

  1. javascript 二维(多维)数组的复制问题

    最近在项目中遇到一个动画暂停的效果,需要在动画停止的时候检测当前坐标和已经运行的时间,从而调节时间轴为再次运行时加速. 但是在数组保存方面折腾了半天. var orbitArray = [], lin ...

  2. gulp一般使用

    gulp的基本使用总结了一下几点: 1.gulp-ejs的使用 [ file include,html文件合并 ]: let ejs = require("gulp-ejs"); ...

  3. 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan(四)

    功能说明 该步骤实现的功能包括: 1. 启动程序时,将@ComponentScan加载的类,创建对象并放在容器里面. 2. 通过ApplicatoinContext的getBean()方法获得容器里面 ...

  4. C#动态获取鼠标坐标

    .Net封装好的方法 int Control.MousePosition.X;int Control.MousePosition.Y; 用API方法 using System.Runtime.Inte ...

  5. Gitpage + hexo(3.0以上)搭建博客

    大半天,一边折腾,一边查找各种文档,写出的这篇文档,不知道有没有把程序表示得足够简明,有不足之处望指明. 前提:已安装好nodeJS和git. 桌面右击进入gitbash,输入npm install ...

  6. mongoose返回值无法修改

    mongoose 查询方法 find 例:db.collections.find(query,function(err,doc) { 如果var res = doc[0]  是{name:'feife ...

  7. Logstash&Redis&Elasticsearch&Kibana

    [搭建] 一个很好的提示,强调版本的一致性 http://www.cnblogs.com/yjf512/p/4194012.html http://michael.bouvy.net/blog/en/ ...

  8. 【转】用PowerDesigner制作数据库升级脚本

    [原创]用PowerDesigner制作数据库升级脚本   很多人使用PD的时候就问有没有制作自动升级脚本的功能.其实是有的. 操作原理: 1.保存原来的版本,另存为apm的文件,生成一个Archiv ...

  9. Unity3D获取资源的方法整理:

    在使用Unity3D做项目时,获取资源的方法大致分为两种.一种是通过写代码的方式,在程序运行时,自动获取资源:一种是通过手动拖拽的方式进行获取.不管是什么类型的资源都能通过这两种方式获得,下面拿图片资 ...

  10. gulp基础操作实践

    按照gulp中文文档对gulp基础操作的一些实践练习,记录以防忘掉. 一,选择并输出文件:gulp.src(globs[,options]) eg:gulp.src('src/less/index.l ...