hdu4923 Room and Moor 单调栈

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 = {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
 
  多校第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-10
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值