hdu4923(2014多校第六场第三题)

来源:互联网 发布:布兰诗歌 知乎 编辑:程序博客网 时间:2024/06/15 05:02

Room and Moor

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


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
 

贪心,注意一堆0前面的一堆1的x值一定相等,然后求出每一堆的x,如果前面x大于现在的,就把前面的和现在的合并,一直贪心下去即可。

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int u[100005],x[100005],y[100005];double a[100005],b[100005];int tear;int main(){    int i,n,t,x1,y1;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(i=1;i<=n;i++){            scanf("%d",&u[i]);        }        tear=0;        i=1;        while(i<=n){            x[tear]=0;            y[tear]=0;            while(i<=n&&u[i]==1){                x[tear]++;                i++;            }            while(i<=n&&u[i]==0){                y[tear]++;                i++;            }            a[tear]=(double)(x[tear])/(y[tear]+x[tear]);            b[tear]=x[tear]*(1-a[tear])*(1-a[tear])+y[tear]*a[tear]*a[tear];            while(tear>=1){                if(a[tear-1]>a[tear]){                    x1=x[tear-1]+x[tear];                    y1=y[tear-1]+y[tear];                    a[tear-1]=(double)x1/(x1+y1);                    b[tear-1]=x1*(1-a[tear-1])*(1-a[tear-1])+y1*a[tear-1]*a[tear-1];                    x[tear-1]=x1;                    y[tear-1]=y1;                    tear--;                }                else break;            }            tear++;        }        double s=0;        for(i=0;i<tear;i++){            s+=b[i];        }        printf("%.6lf\n",s);    }    return 0;}


0 0
原创粉丝点击