Hdu 4923(单调栈)
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 842 Accepted Submission(s): 250Problem DescriptionPM 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:
InputThe 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.OutputOutput the minimal f (A, B) when B is optimal and round it to 6 decimals.Sample Input4
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 1Sample Output1.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(单调栈)的更多相关文章
- hdu 4923 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...
- hdu 1506 单调栈问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- hdu 5875(单调栈)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5033 (单调栈维护凸包) Building
题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...
- hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1505 单调栈升级版
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- hdu 1506 单调栈
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- HDU 4923 Room and Moor (单调栈)
题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...
- HDU 5033 Building(单调栈)
HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...
随机推荐
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- ElasticSearch入门介绍之会当凌绝顶(一)
ElasticSearch也是一款非常优秀的开源的全文检索框架,以大名鼎鼎的Apache Lucene为基础,高度封装了更丰富,易用的API,同时与Apache Solr一样,提供了非常强大的分布式集 ...
- hammer.js使用
手势包括点击(tap),长按(press),滑动(swipe),方向(pan) 使用实例: <!DOCTYPE html> <html> <head> <me ...
- Quick BI 的模型设计与生成SQL原理剖析
一.摘要 随着物联网的告诉发展,数据量呈现井喷式的增长,如何来分析和使用这些数据,使数据产生商业价值,已经变得越来越重要.值得高兴的是,当前越来越多的人已经意识到了用数据分析决定商业策略的重要性,也都 ...
- 【DM642学习笔记九】XDS560仿真器 Can't Initialize Target CPU
以前用的瑞泰的ICETEK-5100USB仿真器,现在换成XDS560试了试,速度快多了.把720*576的图片在imgae中显示也只需要四五秒钟.而5100仿真器需要三四分钟. 仿真器驱动安好后,刚 ...
- PHP基于openssl实现的非对称加密操作
使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和php的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...
- $(...).live is not function
项目中引入了一个插件,但是调用的时候就报了$(...).live is not function 上网搜索了一下live方法在1.9中被删除了,因为平时自己用的时候就用on的方法,没用过live,所以 ...
- css3之弹性盒模型(Flex Box)
CSS3 弹性盒子(Flex Box) 弹性盒子是 CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时 ...
- 【python之路面向对象】初级篇
概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...
- bzoj 3033 太鼓达人——欧拉图搜索
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3033 思路:肯定是把2^m个数当成点,每个点连了2条入边.2条出边,然后求一个经过所有点一次 ...
