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

来源:互联网 发布:outlook邮件搜索软件 编辑:程序博客网 时间:2024/05/11 00:25


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 = {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
491 1 1 1 1 0 0 1 191 1 0 0 1 1 1 1 140 0 1 140 1 1 1
 

Sample Output
1.4285711.0000000.0000000.000000
 

Author
BUPT
 

Source
2014 Multi-University Training Contest 6



题意:给定一个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 falsetypedef 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 100005int 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