HDU 4923 Room and Moor (多校第六场C题) 单调栈
to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies
that:
![](http://acm.hdu.edu.cn/data/images/C507-1003-1.jpg)
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.
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
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题) 单调栈的更多相关文章
- 2014 HDU多校弟六场J题 【模拟斗地主】
这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...
- Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...
- [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)
题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 2020牛客多校第八场K题
__int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...
- HDU 4923 Room and Moor(推理+栈维护)
HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...
- HDU 4923 Room and Moor (单调栈)
题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...
- 【HDU】4923 Room and Moor(2014多校第六场1003)
Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
随机推荐
- 安全框架Shiro入门
Shiro简介 Apache Shiro是Java的一个安全框架,官网为shiro.apache.org,主要场景为控制登陆,判断用户是否有访问某个功能的权限等等. Shiro的核心功能(入门知识,只 ...
- Linux常用命令100个用法
平时用linux时,我有一个习惯就是把遇到的,比较有用,并且容易忘的命令,放到一个文本文件中,没事的时候可以拿出来看看,这样可以加深映像,时间长了这些命令的用法基本上都能掌握了.以下是100个用法,有 ...
- Do you kown Asp.Net Core -- 配置Kestrel端口
Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨平台可运行 Asp.Net Core 的web服 ...
- c#把汉字转化成全拼音函数(全拼)
/// <summary> /// 把汉字转换成拼音(全拼) /// </summary> /// <param name=&q ...
- Quartus FFT IP核简介
为了突出重点,仅对I/O数据流为steaming的情况作简要说明,以便快速上手,有关FFT ip核模型及每种设置详细介绍请参考官方手册FFT MegaCore Function User Guide. ...
- 使用dropwizard(6)-国际化-easy-i18n
前言 Dropwizard官方文档并没有提供国际化的模块,所以只能自己加.Spring的MessageResource用的很顺手,所以copy过来. Easy i18n 在整合Dropwizard的时 ...
- PHP date()函数格式与用法汇总
在页面的最前页加上 date_default_timezone_set("PRC"); /*把时间调到北京时间,php5默认为格林威治标准时间*/ date () a: & ...
- js 图片转换为base64
<input id="file" type="file"> <img id="img" style="max-h ...
- linux服务器使用yum安装nginx
一,安装nginx和php-fpm yum install nginx php-fpm 二, 找到nginx.conf find / -name nginx.conf 三,添加解析php配置 在ser ...
- HTML基础下
知识点一: HTML5的标准结构: <!DOCTYPE html> <html lang='en'> <head> <meat charset='utf-8' ...