首页 > 代码库 > HDOJ 4923 Room and Moor
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
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
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
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。