Hdu 4932 Room and Moor(数学)

http://acm.hdu.edu.cn/showproblem.php?pid=4923

Room and Moor

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


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
 

Source
题意:给一个长度为N的数组A,Ai只能为0或1。让你构造一个长度也为N的数组B。B数组必须满足以下条件:

求f(A,B)最小为多少?
题解:对于A数组开头连续的0,B数组一定填0。同理对于A数组末尾连续的1,B数组对应的也一定填1。除开首尾的特殊情况剩下的串可以分为111...000..由1和0组成的块。可以贪心的证明出对于每个块B数组填的数字是一样的,因此可以算出对于每个块来说要让它最优填的数字是(该串中1的个数)/(该串的长度)。现在从左到右处理每一块。如果当前块的最优值大于等于前一块的最优值,那么当前块的值就可以填最优值。反之,就不行了,因为B数组必须不降。这时我们需要调整该块要填的值和前一块要填的值。因为对于每一块的 f 值其实是随X变化的二次函数(X为该块所填的数)。那么假设后一块的值固定的情况下,由于前一块要小于等于该值,显然前一块越大越优。所以两个块最后所填的数为相同的情况下最优。那么我们可以把两个块合并成一个,并算出新块的最优解。然后将新块与前面的块比较,需要合并就继续合并。(我们可以用一个栈来维护每个块的最优解)这样我们最后就能靠谱的求出最优解。总结一下,做一道题应该仔细对其进行分析想出靠谱的算法,而一味的乱搞只是在浪费时间。代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#define nn 110000
#define inff 0x3fffffff
#define mod 1000007
using namespace std;
typedef __int64 LL;
int n;
double a[nn];
int l[nn],y[nn];
struct node
{
    int l,r;
    double val;
}tem,ix;
stack<node>sta;
int main()
{
    int t,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(l,0,sizeof(l));
        memset(y,0,sizeof(y));
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&a[i]);
            l[i]=l[i-1];
            y[i]=y[i-1];
            if(a[i]==1.0)
                y[i]++;
            else
                l[i]++;
        }
        tem.l=1,tem.r=1,tem.val=a[1];
        sta.push(tem);
        node fc;
        for(i=2;i<=n;i++)
        {
            fc.l=i,fc.r=i,fc.val=a[i];
            while(1)
            {
                if(sta.empty())
                {
                    sta.push(fc);
                    break;
                }
                ix=sta.top();
                if(fc.val>=ix.val)
                {
                    sta.push(fc);
                    break;
                }
                sta.pop();
                fc.l=ix.l;
                fc.val=(y[fc.r]-y[fc.l-1])*1.0/((fc.r-fc.l+1)*1.0);
            }
        }
        double ans=0;
        while(sta.size())
        {
            ix=sta.top();
            sta.pop();
            ans+=(1.0-ix.val)*(1.0-ix.val)*(y[ix.r]-y[ix.l-1])+ix.val*ix.val*(l[ix.r]-l[ix.l-1]);
        }
        printf("%.6lf\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值