hdu 4923 Room and Moor

Room and Moor

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


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.


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
using namespace std;
const double eps=1e-9;
int n;
int a[100010];
struct node{
    int cnt;
    int all;
    double b;
    node(int cnt=0,int all=0,double b=0):cnt(cnt),all(all),b(b){}
};
vector<node>cur;
node res[100010];

void deal_pre(){
    cur.clear();
    while(n>0 && a[n]==1) n--;
    for(int i = 1; i <= n; i++){
        if(a[i] == 1){
            int j = i;
            int cnt = 0;
            while(j<=n && a[j]==1) j++,cnt++;
            while(j<=n && a[j]==0) j++;
            double b = cnt * 1.0 / (j-i);
            cur.push_back(node(cnt, j-i, b));
            i=j-1;
        }
    }
}
int sign(double x){
    if(x > eps) return 1;
    else if(x < -eps) return -1;
    return 0;
}
//int binary(int ed, double x){
//    int st = 0, mid ;
//    while(st + 1 < ed){
//        mid = ( st + ed ) >> 1;
//        if( sign( x - res[mid].b )>= 0 ) st = mid;
//        else ed = mid;
//    }
//    if( sign( x - res[st].b) == 0 ) st++;
//    return st;
//}

int deal_res(int len){
    int i = 0;
    res[0] = cur[i++];
    int ed = 0;
    while(i < len){
        if(sign( cur[i].b - res[ed].b ) >= 0){
            res[++ed] = cur[i];
        }
        else{
//            int st = binary(ed,cur[i].b);

//            for(int iy = st; iy <= ed; iy++){
//                sum += res[iy].all;
//                cnt += res[iy].cnt;
//            }
//            ed = st;
            int sum = cur[i].all;
            int cnt = cur[i].cnt;
            while(ed >= 0){
                sum += res[ed].all;
                cnt += res[ed].cnt;
                double tp = 1.0 * cnt / sum;
                if( ed == 0){
                    res[ed]= node(cnt,sum,tp);
                    break;
                }
                else{
                    if(tp >= res[ed-1].b){
                        res[ed] = node(cnt , sum, tp);
                        break;
                    }
                    else ed--;
                }

            }
        }
        i++;
    }
    return ed;
}
double work(){
    int len = cur.size();
    if(len == 0) return 0;
    int ed = deal_res(len);
    double ans = 0;
    for(int i = 0; i <= ed; i++){
        int all = res[i].all;
        int cnt = res[i].cnt;
        double b1 = (all - cnt) * 1.0 / all;
        double b2 = cnt * 1.0 / all;
        ans = ans + cnt * b1 * b1 + (all - cnt) * b2 * b2;
    }
    return ans;
}
int main()
{

//	freopen("in.in","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);

        deal_pre();
        printf("%.6f\n",work());
    }

	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值