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 ...
随机推荐
- Django项目: 3.用户注册功能
本章内容的补充知识点 导入库的良好顺序: 1.系统库 2.django库 3.自己定义的库(第三方库) redis缓存数据库的数据调用速度快,但是不利于长时间保存. mysql用于长时间存储,但是调用 ...
- 利用jQuery获取jsonp
前端js代码: $.ajax({ url: 'http://localhost:8080/webApp/somejsonp', dataType: "jsonp", jsonp: ...
- 廖雪峰Java10加密与安全-5签名算法-2DSA签名算法
DSA DSA:Digital Signature Algorithm,使用EIGamal数字签名算法,和RSA数字签名相比,DSA更快. DSA只能配合SHA使用: SHA1withDSA SHA2 ...
- Django项目:CRM(客户关系管理系统)--80--70PerfectCRM实现CRM业务流程(bpm)课程排行分页
# coursetop_views.py # ————————64PerfectCRM实现CRM课程排名详情———————— #————班级学生详情——#计算#{学员ID:分数}——#计算 #{学员I ...
- JEECG-Boot开发环境准备(三):开发环境搭建
目录索引: 前端开发环境搭建 安装开发工具 导入项目 后端开发环境搭建 安装开发工具 导入项目 第一部分: 前端开发环境搭建 一.安装开发工具 安装nodejs.webstrom.yarn,安装方法参 ...
- Configuring to Debug and Workaround Broken Client Applications
背景:C3P0数据库连接池占满 Configuring to Debug and Workaround Broken Client Applications http://www.mchange.co ...
- [Day6] Nginx 进阶模块
一. 使用变量防盗链referer模块 功能:通过验证referer请求头是否合法,来拒绝非正常的网站访问我们站点的资源 思路:通过referer模块,用invaild_refereri变量根据配置判 ...
- docker tomcat启动慢
镜像 https://hub.docker.com/r/errorlife/tomcat/ docker pull errorlife/tomcat
- js 之 call 、 apply
在学习js过程中怎么也绕不过用到call.apply方法,感觉都差不多,现在看看他们的用法,区别 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(conte ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...