【HDU4923Room and Moor】栈模拟 区间均值


Room and Moor

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


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



题意:给定一个01组成的a序列,要求一个b序列,b序列每个数值为[0, 1]之间的数,并且b序列为非递减序列,要求(aibi)2最小,求这个最小值

思路:推理,很容易看出,开头一段的0和末尾一段的1等于没有,然后中间每段类似111000这样1在前,0在后的序列,都可以列出一个公式,很容易推出选择的x为共同的一个值,为1的个数/(1的个数+0的个数)a,那么问题就变成要维护一个递增的x,利用一个栈去做维护,如果遇到一个位置递减了,那么就把它和之前的段进行合并,维护栈中递增,最后把栈中元素都拿出来算一遍就是答案了



写的比较复杂=。=

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
int a[N];
int b[N];
int n;
double get(int a, double b)
{
    return (a - b) * (a - b);
}
double f(double B, int l, int r)
{
    double ans = 0;
    for (int i = l; i <= r; i++)
    {
        ans += get(b[i], B);
    }
    return ans;
}
double sum[N];

struct data
{
    double sum;
    double n;
    double avg;
    int l, r;
    data()
    {
        sum = 0;
        n = 0;
        avg = 0;
        l = INF;
        r = 0;
    }
};
data D[N];
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int sumn = 0;
        scanf("%d", &n);
        int l = 0;
        int r = n - 1;
        memset(sum, 0, sizeof(sum));
        memset(D, 0, sizeof(D));
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        while (l < n && !a[l])
        {
            l++;
        }
        while (r >= 0 && a[r])
        {
            r--;
        }
        int nn = 0;
        for (int i = l; i <= r; i++)
        {
            b[nn++] = a[i];
        }
        if (nn == 0)
        {
            printf("%.6lf\n", 0.0);
        }
        else
        {
            double nowsum = 0;
            int nown = 0;
            bool flag1 = true;
            bool flag0 = false;
            for (int i = 0; i < nn; i++)
            {
                if (b[i] && flag1)
                {
                    nowsum += 1;
                    nown++;
                }
                else
                {
                    flag1 = false;
                    flag0 = true;
                }
                if (!b[i] && flag0)
                {
                    nown++;
                }
                else
                {
                    flag0 = false;
                }
                if (!flag0 && !flag1)
                {
                    sum[sumn] = nowsum / nown;
                    D[sumn].sum = nowsum;
                    D[sumn].n = nown;
                    D[sumn].l = i  - nown;
                    D[sumn].r = --i;
                    D[sumn].avg = sum[sumn];
                    sumn++;
                    flag1 = true;
                    flag0 = false;
                    nowsum = 0;
                    nown = 0;
                }
            }
            if (nown != 0)
            {
                sum[sumn] = nowsum / nown;
                D[sumn].sum = nowsum;
                D[sumn].n = nown;
                D[sumn].l = nn  - nown;
                D[sumn].r = nn - 1;
                D[sumn].avg = sum[sumn];
                sumn++;
            }

            stack<data>ST;
            for (int i = 0; i < sumn; i++)
            {
                // printf("%lf\n", sum[i]);
                if (ST.empty())
                {
                    ST.push(D[i]);
                    continue;
                }
                if (ST.top().avg > sum[i])
                {
                    ST.push(D[i]);
                    data now;
                    data pushit;
                    while(!ST.empty())
                    {
                        now = ST.top();
                        if(now.avg<pushit.avg)
                            break;
                        pushit.sum += now.sum;
                        pushit.n += now.n;
                        pushit.l=min(pushit.l,now.l);
                        pushit.r = max(pushit.r, now.r);
                        pushit.avg = pushit.sum / pushit.n;
                        ST.pop();
                    }
                    ST.push(pushit);
                }
                else
                {
                    ST.push(D[i]);
                }
            }
            double ans = 0;
            data now;
            while (!ST.empty())
            {
                now = ST.top();
                ans += f(now.avg, now.l, now.r);
                ST.pop();
            }
            printf("%.6lf\n", ans);
        }
    }

    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值