用一个栈维护b的值,每次把一个数放到栈顶。

看栈首的值是不是大于这个数,假设大于的话将栈顶2个元素合并。b的值就是这两个栈顶元素的平均值。

。。

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 943    Accepted Submission(s): 291

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
 
Author
BUPT
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath> using namespace std; const double eps=1e-8; double a[110000],b[110000]; typedef pair<double,double> pDD; int n;
stack<pDD> STACK; int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
while(STACK.size()) STACK.pop();
for(int i=0;i<n;i++)
{
scanf("%lf",a+i);
pDD A=pDD(a[i],1);
while(STACK.size()&&A.first+eps<STACK.top().first)
{
pDD B=STACK.top(); STACK.pop();
double Sec=A.second+B.second;
double Fst=(A.first*A.second+B.first*B.second)/Sec;
A.first=Fst; A.second=Sec;
}
STACK.push(A);
} int now=n-1;
while(STACK.size())
{
pDD u=STACK.top(); STACK.pop();
int sz=u.second;
for(int i=now,j=0;i>=0&&j<sz;i--,j++)
{
b[now--]=u.first;
}
} double ans=0;
for(int i=0;i<n;i++)
{
ans+=(a[i]-b[i])*(a[i]-b[i]);
}
printf("%.6lf\n",ans);
}
return 0;
}

HDOJ 4923 Room and Moor的更多相关文章

  1. hdu 4923 Room and Moor [ 找规律 + 单调栈 ]

    传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  2. HDU 4923 Room and Moor

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1.     ...

  3. 【HDU】4923 Room and Moor(2014多校第六场1003)

    Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  4. HDU 4923 Room and Moor (多校第六场C题) 单调栈

    Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. ...

  5. HDU 4923 Room and Moor(推理+栈维护)

    HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...

  6. HDU 4923 Room and Moor(瞎搞题)

    瞎搞题啊.找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和. 然后从前向后扫一遍.变扫边进行合并.每次合并.合并的是他的前驱.这样到最后从t-1找出的那条链就是 ...

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

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

  8. hdu 4923 Room and Moor (单调栈+思维)

    题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...

  9. Hdu 4923(单调栈)

    题目链接 Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

随机推荐

  1. 为什么Linux的Fdisk分区时First Sector为2048?

    是为EFI启动预留的,而Fdisk也随着这波的兴起而跟着变. 参考: http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/disc-pa ...

  2. 删除VisualStudio 2013中的 "send Feedback" 按钮

    在VisualStudio 2013中,在标题栏中增加了一个 "send Feedback" 按钮,用于给微软发送Bug和回馈(或者一个哭脸和笑脸).这个按钮对于开发来说基本上没用 ...

  3. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  4. SilverLight-3:目录

    ylbtech-SilverLight-Index: 1.A,返回顶部 Layout The Layout Containers - The Panel Background Borders   Si ...

  5. python字典里的update函数

    >>> print d {'age': 34, 'name': 'jeapedu'} >>> d1={"age":38} >>> ...

  6. 一分钟学awk够用

    [转载于58同城沈剑] 1.什么是AWK(1)Aho.Weinberger.Kernighan三位发明者名字首字母:(2)一个行文本处理工具: 2.AWK基本原理2.1原理:逐行处理文件中的数据 2. ...

  7. iptables 使用场景

    25 Most Frequently Used Linux IPTables Rules Examples by RAMESH NATARAJAN on JUNE 14, 2011 At a firs ...

  8. Elasticsearch 的 Update更新

    Update更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更新.可以把它看成是先删除再索引的原子操作,只是省略了返回的过程,这样即节省了来回传输的网络流量,也避免了中间时间造成的文 ...

  9. Html5 Canvas之arc函数图示详解

    在不变换坐标系的情况下,arc函数画弧的方向如下图所示: 举例来说,arc(x,y,r,Math.PI/6,Math.PI/3,false);是以x,y为圆心,r为半径,从x正轴顺时针30度为起始角, ...

  10. Android Socket通信编程

    安卓客户端通过socket与服务器端通讯一般可以按照以下几个步骤:(1).通过IP地址和端口实例化Socket,请求连接服务器:socket = new Socket(HOST, PORT); //h ...