hdu4923 Room and Moor 单调栈

来源:互联网 发布:游戏官方网站源码 编辑:程序博客网 时间:2024/05/21 08:41

Room and Moor

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


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
 
  多校第6场,前两题出的还挺快,后面就呵呵了=。=

  先把前面的1和后面的0去掉。

  我当时想的是会不会就取0...x...1这样子,结果不对。。

  正确答案是0...x1...x2...x3....1,中间可能有很多取值,满足x1<x2<x3...先把剩下的序列分成一段一段的,连续的1和连续的0为一段,求一个x,为什么连续的1和连续的0为一段呢,因为如果把连续1看成一段,连续0看成一段,1的取值是1,x的取值是0,0<1,所以肯定还是要合并的。为什么这一段只取一个x呢,因为1的段希望x尽量大,0的段希望x尽量小,0段的取值又不能小于1段的取值,所以相等是最优了。x的算法根据抛物线,-b/(2a)。每段求出来的x放到栈里,如果比上段的x小,就要和上段合并,把这段和上段看成一段,再用同样的方法算x,如果又比上段小,继续合并。。

 

#include<iostream>#include<algorithm>#include<queue>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<set>#include<map>#include<stack>#define INF 0x3f3f3f3f#define MAXN 100010#define MAXM 1500#define MOD 1000000007#define MAXNODE 8*MAXN#define eps 1e-10using namespace std;typedef long long LL;int T,N,a[MAXN],cnt1[MAXN];struct seg{    int s,e;    double x;};stack<seg> st;int main(){    freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--){        scanf("%d",&N);        for(int i=1;i<=N;i++) scanf("%d",&a[i]);        int p1,p2,flag1=0,flag2=0;        for(int i=1;i<=N;i++) if(a[i]!=0){            p1=i;            flag1=1;            break;        }        if(!flag1){            printf("0.000000\n");            continue;        }        for(int i=N;i>=1;i--) if(a[i]!=1){            p2=i;            flag2=1;            break;        }        if(!flag2){            printf("0.000000\n");            continue;        }        if(p1==p2+1){            printf("0.000000\n");            continue;        }        cnt1[0]=0;        for(int i=1;i<=N;i++){            cnt1[i]=cnt1[i-1];            if(a[i]==1) cnt1[i]=cnt1[i-1]+1;        }        while(!st.empty()) st.pop();        int s,e,i=p1;        while(i<=p2){            for(s=i;s<=p2&&a[s]==1;s++);            for(e=s;e+1<=p2&&a[e+1]==0;e++);            double A=e-i+1,B=-2*(cnt1[e]-cnt1[i-1]),x=(-B)/(2*A);            int ls=i;            while(!st.empty()&&x<st.top().x){                ls=st.top().s;                st.pop();                A=e-ls+1,B=-2*(cnt1[e]-cnt1[ls-1]),x=(-B)/(2*A);            }            st.push((seg){ls,e,x});            i=e+1;        }        double ans=0;        while(!st.empty()){            seg tmp=st.top();            st.pop();            double A=tmp.e-tmp.s+1,B=-2*(cnt1[tmp.e]-cnt1[tmp.s-1]),C=cnt1[tmp.e]-cnt1[tmp.s-1];            ans+=(4*A*C-B*B)/(4*A);        }        printf("%.6lf\n",ans);    }    return 0;}


0 0
原创粉丝点击