HDOJ 4923 Room and Moor
来源:互联网 发布:淘宝已发货怎么改物流 编辑:程序博客网 时间:2024/05/07 16:00
用一个栈维护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.
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
491 1 1 1 1 0 0 1 191 1 0 0 1 1 1 1 140 0 1 140 1 1 1
Sample Output
1.4285711.0000000.0000000.000000
Author
BUPT
Source
2014 Multi-University Training Contest 6
#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;}
1 0
- hdoj 4923 Room and Moor
- HDOJ 4923 Room and Moor
- [HDOJ 4923] Room and Moor [单调栈]
- HDOJ 4923 Room and Moor(求方差、栈)
- HDU 4923 Room and Moor
- HDU 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- HDU-4923-Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- Hdu 4923 Room and Moor
- HDU 4923 Room and Moor
- hdu 4923 Room and Moor(线性表)
- hdu 4923 Room and Moor 堆栈
- HDU 4923 Room and Moor(瞎搞题)
- 【TOJ 2871】Magic Bean【DP+矩阵快速幂】
- 各种排序算法的比较
- Error creating bean with name 'adminUserController': Injection of autowired dependencies failed;
- Java 线程池技术之一 自实现线程池
- 9.jQuery UI 验证插件
- HDOJ 4923 Room and Moor
- HDU-1908-Double Queue
- hdu 1394 Minimum Inversion Number(逆序数)
- 光影切割问题-求逆序对@编程之美
- 数学的分支
- 深度分析如何在Hadoop中控制Map的数量
- oracle存储过程
- Android 自定义裁剪实现
- hdu3270 The Diophantine Equation