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. 面向亿万级用户的QQ一般做什么?——兴趣部落的Web同构直出分享

    作者:李强,腾讯web开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/348.html 一.什么是同构 ...

  2. 基于gitHub+hexo搭建的个人博客

    文章导航 前期准备 安装hexo 修改hexo主题 自定义主题 部署本地文件到github查看 我的第一篇博客 前期准备 下载安装git命令行工具.node及npm环境 注册自己的GitHub账号 安 ...

  3. Android Weekly Notes Issue #283

    November 12th, 2017 Android Weekly Issue #283 本期内容包括Gradle相关的几篇,如封装繁杂依赖的技巧,通过kotlin dsl让gradle支持kotl ...

  4. IdentityServer4(10)- 添加对外部认证的支持之QQ登录

    前言 前面我们提到过IdentityServer4是可以添加外部认证的,如果外部认证支持OAuth2,那么添加到IdentityServer4是非常简单的,在ASP.NET Core下提供了非常多的外 ...

  5. 小随笔:利用Shader给斯坦福兔子长毛和实现雪地效果

    0x00 前言 发现最近没有了写长篇大论的激情,可能是到了冬天了吧.所以这篇小文只是简单介绍下如何在Unity中利用shader很简单的实现雪地效果以及毛皮效果,当然虽然标题写在了一起,但其实这是俩事 ...

  6. flask + Python3 实现的的API自动化测试平台---- IAPTest接口测试平台

    **背景: 1.平时测试接口,总是现写代码,对测试用例的管理,以及测试报告的管理持久化做的不够,              2.工作中移动端开发和后端开发总是不能并行进行,需要一个mock的依赖来让他 ...

  7. 【smart-transform】取自 Atom 的 babeljs/coffeescript/typescript 智能转 es5 库

    简介 有时间研究下开源库的源码,总是会有些收获的.注意到 Atom 插件编写时,可以直接使用 babel, coffeescript 或者 typescript.有些诧异,毕竟 Electron 中内 ...

  8. 数细胞-swust oj

    数细胞(0964) 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.编程需要用到的队列及其相关函数已经实现,你只需要完 ...

  9. Android截屏的几种实现

    Android截屏的几种实现 微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521 最近我们的APP要求需要截屏功能,网上看了看大致有 ...

  10. HBase表预分区与压缩

    1.建立HBase预分区表.sql语句如下: create 'buyer_calllogs_info_ts', 'record', {SPLITS_FILE => 'hbase_calllogs ...