标签:des style http color java os io strong
用一个栈维护b的值,每次把一个数放到栈顶。看栈首的值是不是大于这个数,如果大于的话将栈顶2个元素合并,b的值就是这两个栈顶元素的平均值。。。
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,布布扣,bubuko.com
标签:des style http color java os io strong
原文地址:http://blog.csdn.net/ck_boss/article/details/38470237