本文主要是介绍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 = {A 1, A 2,..., A N}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B 1, B 2,... , B N} 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 A i.
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 A i.
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;
}
这篇关于HDOJ 4923 Room and Moor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!