Hdoj 4923 room and moor

Source: Internet
Author: User


Maintain the value of B with one stack, and place a number to the top of the stack each time. Check whether the value at the beginning of the stack is greater than this number. If it is greater than this number, merge the two elements at the top of the stack. The value of B is the average value of the two elements at the top of the stack...

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 descriptionpm 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:

 
Inputthe 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.
Outputoutput 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
 
Authorbupt
Source2014 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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.