题目链接

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 842    Accepted Submission(s): 250

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

 Accepted Code:

 /*************************************************************************
> File Name: 4923.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月08日 星期五 22时27分38秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const double eps = 1e-;
const int maxn = ;
int n;
int a[maxn], st[maxn]; struct node {
double x; //区间均值
int l, r, one; //区间范围及1的个数
}A[maxn]; // unite i to j
void unite(int i, int j) {
A[j].l = A[i].l;
A[j].x = A[j].one + A[i].one + 0.0;
A[j].x /= A[j].r - A[j].l + ;
A[j].one = A[i].one + A[j].one;
} double solve() {
int len = ;
for (int i = ; i <= n; i++) {
A[len].x = a[i] + 0.0;
A[len].l = i;
i++;
while (i <= n && a[i] == a[i-]) i++;
A[len].r = i - ;
i--;
if (a[i]) A[len].one = A[len].r - A[len].l + ;
else A[len].one = ;
len++;
}
int top = ;
for (int i = ; i < len; i++) {
while (top && A[i].x < A[st[top-]].x) {
unite(st[top-], i);
top--;
}
st[top++] = i;
}
double ans = 0.0;
for (int i = ; i < top; i++) {
for (int j = A[st[i]].l; j <= A[st[i]].r; j++) {
ans += (a[j] - A[st[i]].x) * (a[j] - A[st[i]].x);
}
}
return ans + eps;
} int main(void) {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", a + i);
printf("%.6f\n", solve());
}
return ;
}

Hdu 4923(单调栈)的更多相关文章

  1. hdu 4923 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...

  2. hdu 1506 单调栈问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...

  3. hdu 5033 单调栈 ****

    看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...

  4. hdu 5875(单调栈)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. HDU 5033 (单调栈维护凸包) Building

    题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...

  6. hdu 3410 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...

  7. hdu 1505 单调栈升级版

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

  8. hdu 1506 单调栈

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

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

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

  10. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

随机推荐

  1. Java(8)中List的遍历方式总结

    本篇文章主要讲述了List这一集合类型在Java,包括Java8中的遍历方式,不包括其他的过滤,筛选等操作,这些操作将会在以后的文章中得到提现,由List可以类推到Set等类似集合的遍历方式. pub ...

  2. windows 常用的快捷键

    记录一些 windows 常用快捷键,待更新 Ctrl系列 快捷键 功能 Ctrl + C 复制 Ctrl + INSERT 复制 Ctrl + V 粘贴 Ctrl + Z 撤销 Ctrl + D 删 ...

  3. 跟我一起在ubuntu中安装docker

    卸载旧版本 $ sudo apt-get remove docker docker-engine docker.io 查看ubuntu版本 设置安装源 通过如下步骤,设置安装源仓库,这里我们使用阿里源 ...

  4. [Swoole系列入门教程 4] 定时器与心跳demo

  5. [转]WPF自定义控件之带倒计时的按钮--Button

    1.说明 之前做过一个小项目,点击按钮,按钮进入倒计时无效状态,计时完成后,恢复原样,现在就实现该效果---带倒计时的按钮 2.效果 1)正常状态               2)MouseOver( ...

  6. python 解决抓取网页中的中文显示乱码问题

    关于爬虫乱码有很多各式各样的问题,这里不仅是中文乱码,编码转换.还包括一些如日文.韩文 .俄文.藏文之类的乱码处理,因为解决方式是一致的,故在此统一说明. 网络爬虫出现乱码的原因 源网页编码和爬取下来 ...

  7. 全栈之路-微信小程序-SKU开发(代码)

    SKU开发是小程序中最难的一部分,思路在分析中已经记录过了,这里主要看一下代码的实现,感觉老师写的代码太棒了,很优雅!主要想记录一下写代码的思路,对面向对象编程的实践. 一.代码结构的分析 1.说明几 ...

  8. day20 作业

    目录 今日作业 1.下面这段代码的输出结果将是什么?请解释. 2.多重继承的执行顺序,请解答以下输出结果是什么?并解释. 3.什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先 ...

  9. LUGOU P3374 【模板】树状数组 1(CDQ 分治)

    传送门 拿个二维偏序练练cdq板子,其实就和归并排序差不多,复杂度不太会,似乎nlogn?. #include<iostream> #include<cstdio> #incl ...

  10. 写js过程中遇到的一个bug

    <div class="func_Div" id="xxcx"><span>信息查询</span>              ...