HDOJ 4923 Room and Moor
用一个栈维护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
which satisfies that:
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.
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
1.428571
1.000000
0.000000
0.000000
#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的更多相关文章
- hdu 4923 Room and Moor [ 找规律 + 单调栈 ]
传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 4923 Room and Moor
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1. ...
- 【HDU】4923 Room and Moor(2014多校第六场1003)
Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- 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. ...
- HDU 4923 Room and Moor(推理+栈维护)
HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...
- HDU 4923 Room and Moor(瞎搞题)
瞎搞题啊.找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和. 然后从前向后扫一遍.变扫边进行合并.每次合并.合并的是他的前驱.这样到最后从t-1找出的那条链就是 ...
- HDU 4923 Room and Moor (单调栈)
题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...
- hdu 4923 Room and Moor (单调栈+思维)
题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得 sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...
- Hdu 4923(单调栈)
题目链接 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
随机推荐
- SQL SERVER 补丁查看
SELECT @@VERSION as 版本情况 SELECT SERVERPROPERTY('ProductVersion') as 产品版本编号, SERVERPROPERTY('Produc ...
- debug with Linux slub allocator
http://thinkiii.blogspot.jp/2014/02/debug-with-slub-allocator.html The slub allocator in Linux has ...
- Jsp中如何在<c:forEach>标签内获取集合的长度
利用jstl标签functions的prefix属性的length属性值 1.首先在jsp页面导入jstl function标签 <%@ taglib prefix="fn" ...
- 走进C++程序世界-----operator new delete 重载
在C++ 的世界里,new 和delete 是keyword.而在C的世界里相相应的malloc和free是函数,关键C++的new和delete分析,详见前面的章节.这里就不在过多的介绍了.链接. ...
- Linux Shell常用技巧
转载自http://www.cnblogs.com/stephen-liu74/ 一. 特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文 ...
- 转:全面分析 Spring 的编程式事务管理及声明式事务管理
转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/
- [PWA] Show an Error when a POST or DELETE Fails in an Offline PWA
We're storing JSON data in the cache - but that only applies to HTTP GET requests - and not to POST ...
- 在web目录下 批量寻找配置文件信息
dir /s /b *.php *.inc *.conf *.config >>list.txt" W4 I2 U+ N/ B6 K @0 r r8 ^ T00LS: _$ j! ...
- Android实现炫酷SVG动画效果
svg是眼下十分流行的图像文件格式了,svg严格来说应该是一种开放标准的矢量图形语言,使用svg格式我们能够直接用代码来描画图像,能够用不论什么文字处理工具打开svg图像.通过改变部分代码来使图像具有 ...
- STL学习笔记(迭代器类型)
迭代器类型 迭代器是一种“能够遍历某个序列内的所有元素”的对象.它可以透过与一般指针一致的接口来完成自己的工作. 不同的迭代器具有不同的”能力“(行进和存取能力) Input迭代器 Input迭代器只 ...