hdu 4923 Room and Moor(数学题)2014多校训练第6场

Room and Moor

                                                                         Time Limit: 12000/6000 MS (Java/Others)    Memory Limit:
262144/262144 K (Java/Others)

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

题意:给出一个只包含0和1的A序列,找出一个B序列,使得f(A,B)最小,且Bi <= B(i+1), 0<= Bi <= 1。f(A,B)的计算方法如上图。

分析:分析可得,当Ai和Bi越接近时,f(A,B)越小。因为Bi<=B(i+1),所以我们可以分段来求。每一段中的Bi尽量取这一段Ai的平均值。但是有可能会出现前面的值大于后面的值的情况,这时,我们只需要把这两段合并为一段,每个Bi取这段Ai的平均值。

#include<cstdio>
const int N = 100010;
int sum[N], seg[N];
typedef __int64 LL;
bool Check_Biger(int a1, int b1, int a2, int b2)
{
    return (LL)(sum[b1] - sum[a1 - 1]) * (b2 - a2 + 1) >=
           (LL)(sum[b2] - sum[a2 - 1]) * (b1 - a1 + 1);
}
int main()
{
    int T, n, i, a;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        sum[0] = 0; //sum[i]表示从A1到Ai一共有多少个1
        for(i = 1; i <= n; i++)
        {
            scanf("%d",&a);
            sum[i] = sum[i-1] + a;
        }
        int length = 0, id;
        for(i = 1; i <= n; i++)
        {
            id = i;
            while(length > 0 && Check_Biger(seg[length], id-1, id, i))
            {  //如果前面一段的平均值不小于后面的平均值,则合并这两段
                id = seg[length--];
            }
            seg[++length] = id;
        }
        seg[++length] = n + 1;
        double ans = 0;
        for(i = 1; i < length; i++)
        {
            int l = seg[i]; //当前段的左端点
            int r = seg[i+1] - 1; //当前段的右端点
            int len = r - l + 1; //当前段的长度
            double x = (double(sum[r]) - sum[l-1]) / len; //当前段每一个元素都取这一段的平均值
            ans += sum[r] - sum[l-1] + len * x * x - 2 * (sum[r] - sum[l-1]) * x;   //(Ai - x)^2
        }
        printf("%lf\n", ans);
    }
    return 0;
}

hdu 4923 Room and Moor(数学题)2014多校训练第6场

时间: 2024-10-01 10:53:46

hdu 4923 Room and Moor(数学题)2014多校训练第6场的相关文章

hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场

Lucky Number                                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description "Ladies and Gentlemen, It's show time! " "A thie

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

hdu 4902 Nice boat(2014多校训练第4场 1006)

Nice boat                                                                           Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is an old country and the king fell in love with a d

hdu 4960 Another OCD Patient(dp)2014多校训练第9场

Another OCD Patient                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) pat

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005

The Romantic Hero                                                                               Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is an old country and the king fell in lov

hdu 4915 Parenthese sequence(模拟)2014多校训练第5场

Parenthese sequence                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description bobo found an ancient string. The string contains only t

hdu 4939 Stupid Tower Defense(DP)2014多校训练第7场

Stupid Tower Defense                                                                         Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description FSF is addicted to a stupid tower defense game. Th

hdu 4927 Series 1(高精度) 2014多校训练第6场

Series 1                                                                            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-o