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 ...
随机推荐
- iOS AudioSession详解 Category选择 听筒扬声器切换
在你读这篇文章之前,如果你不嫌读英文太累,推荐阅读下苹果iOS Human Interface Guidelines中Sound这一章. 选择一个Category AVAudioSessionCa ...
- APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate notice, with the fields e ...
- 【java】java自带的java.util.logging.Logger日志功能
偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logg ...
- ubuntu配置 测试环境 记录
1 更新源 进入 /etc/apt/sources.list sudo vim进入, 更改为如下源 # See http://help.ubuntu.com/community/UpgradeNot ...
- python 文件操作的注意事项
我们经常用python读取文件信息,这里有些注意事项 with open('test.txt', 'r') as f: print 'f.read', f.read() with open('test ...
- ubuntu 查看系统服务的列表
service --status-all
- App开发者博客之: 包建强 (专注移动app开发)
http://www.cnblogs.com/Jax/p/4912606.html 著有"App研发录" 一书. Android EventBus源码解析 带你深入理解EventB ...
- 【高级功能】使用 Ajax
Ajax 是现代Web 应用程序开发的一项关键工具.它让你能向服务器异步发送和接收数据,然后用 Javascript 解析. Ajax 是 Asynchronous JavaScript and XM ...
- 微信小程序 - 一种简写方式
// 第一种 isOrder: function() { //code }, // 第二种 isOrders() { //code },
- react 打包后,项目部署完毕,刷新页面报错(404)
原因解析: 之所以你在浏览器内可以由首页跳转到其他路由地址,是因为这是由前端自行渲染的,你在React Router定义了对应的路由,脚本并没有刷新网页访问后台,是JS动态更改了location. 当 ...