用一个栈维护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. cocurrent包 锁 Lock

    20. 锁 Lock java.util.concurrent.locks.Lock 是一个类似于 synchronized 块的线程同步机制.但是 Lock 比 synchronized 块更加灵活 ...

  2. 在Android App中集成Google登录

    技术文章 来源:码农网 发布:2016-09-19 浏览:194 摘要:今天,几乎所有的web和移动app都自带谷歌和Facebook登录,这对app开发者和用户来说是一个非常有用的功能,因为几乎每个 ...

  3. Camera setParameters(), getParameters(),unlock()三个方法之间的限制关系

    Camera mCamera = Camera.open(); // 第一次调用getParameters()需要在unlock()方法之前否则出现错误 Camera.Parameters param ...

  4. Spring: aop自动标注时出现 “0 formal unbound in pointcut"

    异常代码信息: org.springframework.beans.factory.BeanCreationException: Error creating bean with name forma ...

  5. 新人补钙系列教程之:Molehill底层API中最重要的Context3D

    Context3D,是一个三维空间的处理环境,负责创建并处理三维对象的各个要素如顶点.片段.透视等等,并将处理的结果使用AGAL(Adobe图形汇编语言)上传给显卡进行运算,运算结果最终被回传给CPU ...

  6. mapbox 接入高德矢量地图实战

    Mapbox 作为现如今比较流行的地图框架为我们提供了漂亮的个性化地图,在平常的使用过程中可以方便的接入高德/谷歌等矢量切片地图.由于Mapbox地图数据来源于Open Street Map等国外厂商 ...

  7. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2

    在编写获取路径方法前,我们先把角色须要的动画文件载入进来,角色的文件为png 和 plist格式. player1_anim.png.plist             player1_anim.pn ...

  8. 从git下载一个工程

    1.右键点工程区,选“Import...” 2.Import source 选Git->“Projects from Git” 3.三个选择项中选最后一项CloneURI 4.弹出对话框,URI ...

  9. 在vs2010中编译log4cxx-0.10.0具体方法(从下载、编译、解决错误具体介绍)

    一. 简单介绍 log4cxx是Java社区著名的log4j的c++移植版.用于为C++程序提供日志功能,以便开发人员对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于A ...

  10. 【Android工具类】怎样保证Android与server的DES加密保持一致

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在我们的应用程序涉及到比較敏感的数据的时候,我们一般会对数据进行简单的加密.在与server之间的数据交互中 ...