• 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.
     

    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
     



    #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
    ",ans);
    	}
    	return 0;
    }



  • 相关阅读:
    HTML转换PDF及SWF及图片
    LuceneNET全文检索封装
    网页中文/汉字验证码
    网站帮助系统
    利用享元模式来解决DOM元素过多导致的网页解析慢、卡死的问题
    redis high available solution/ redis 高可用方案
    跨IDC ycache原理和配置说明
    基于模块粒度和用户粒度的灰度发布方案原理&配置说明
    /usr/bin/env python no such file or directory: dos格式导致的!
    yagent使用说明
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7212134.html
Copyright © 2020-2023  润新知