hdu 4923 Room ans Moor

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 602    Accepted Submission(s): 173


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.
 

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
 
比赛的时候没想出来,想复杂了。。

因为A最大为1,所以A^2求和和A是相等的,所以不用记录左右区间。
/***********************************************\
 |Author: Messyidea
 |Created Time: 2014-8-8 10:35:39
 |File Name: 03.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
#define maxn 100005
double len[maxn];
double num[maxn];
int a,n;
int cnt;
int main() {
	//freopen("input.txt","r",stdin); 
    int cas = 0;
    scanf("%d",&cas);
    while(cas --){
        cnt = 0;
        scanf("%d",&n);
        rep(i,n) {
            scanf("%d",&a);
            num[cnt] = a;
            len[cnt ++] = 1;
            while(cnt >= 2){
                if(num[cnt-1]/len[cnt-1] >= num[cnt-2]/len[cnt-2]) break;
                num[cnt-2] += num[cnt-1];
                len[cnt-2] += len[cnt-1];
                cnt--;
            }
        }
        double ans = 0;
        for(int i = 0;i<cnt;++i){
            double t = num[i]/len[i];
            ans += num[i] - 2*t*num[i] + t*t*len[i];
        }
        printf("%.6lf\n",ans);
    }
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值