题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923

解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件:

1.           0 <= B[i] <= 1

2.          B[i] <= B[i+1]

3.          f(A,B) = ∑(i=1-n) (A[i] - B[i])^2

4.          f(A,B)最小是多少

前面的0可以直接去掉,后面的1可以直接去掉,然后剩下中间形如1 1 0 0的序列,在这样的区间中前后的B[i]很明显应该是相等才能保证最小的,

我们可以先分别求出每段这样的区间的B[i],然后维护一个单调队列,当要插入的B[i]大于等于队尾的B[i],则直接插入,当要插入的小于队尾的B[i],

则将队尾的那个区间跟现在要插入的区间合并然后求出一个新的B[i]代替,得到新的B[i]之后注意又要跟队尾比较。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
#define maxn 100005
struct node
{
int a,b;
double x;
}B[maxn];
int A[maxn];
deque<node> que;
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;++i)
scanf("%d",&A[i]);
while(n >= && A[n] == ) //把后面的1去掉
n--;
int s = ;
while(s <= n && A[s] == )
s++;
if(s >= n)
{
printf("%.6lf\n",);
continue;
}
A[] = ;
int f = ;
for(int i = s;i <= n;++i)
{
if(A[i] == )
{
if(A[i-] == && A[i] == )
{
f++;
B[f].a = B[f].b = ; //初始化 }
B[f].a++;
}
else B[f].b++; }
for(int i = ;i <= f;++i) //遍历一遍,计算出每节的x应该是多少
B[i].x = (double)B[i].a / (double)(B[i].a + B[i].b); // for(int i = 1;i <= f;++i)
// printf("a = %d b = %d x = %.6lf\n",B[i].a,B[i].b,B[i].x);
que.clear();
node temp;
deque<node>::iterator iter;
for(int i = ;i <= f;++i) //维护一个单调队列
{
if(que.empty() || B[i].x >= que.begin()->x)
que.push_front(B[i]);
else
{
while(!que.empty() && B[i].x < que.begin()->x)
{
B[i].a += que.begin()->a;
B[i].b += que.begin()->b;
B[i].x = (double)B[i].a / (double)(B[i].a + B[i].b);
que.pop_front();
}
i--; //为了在下一次循环中,把这个节点插入到队列中
}
}
double tot = ;
for(iter = que.begin();iter != que.end();++iter)
tot += ((double)iter->a * (1.0 - iter->x) * (1.0 - iter->x) + (double)iter->b * iter->x * iter->x);
printf("%.6lf\n",tot);
}
return ;
}

HDU 4923 Room and Moor的更多相关文章

  1. HDU 4923 Room and Moor(推理+栈维护)

    HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...

  2. HDU 4923 Room and Moor (单调栈)

    题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...

  3. hdu 4923 Room and Moor [ 找规律 + 单调栈 ]

    传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  4. HDU 4923 Room and Moor (多校第六场C题) 单调栈

    Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. ...

  5. HDU 4923 Room and Moor(瞎搞题)

    瞎搞题啊.找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和. 然后从前向后扫一遍.变扫边进行合并.每次合并.合并的是他的前驱.这样到最后从t-1找出的那条链就是 ...

  6. hdu 4923 Room and Moor (单调栈+思维)

    题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...

  7. 【HDU】4923 Room and Moor(2014多校第六场1003)

    Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  8. hdu 4923 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...

  9. Hdu 4923(单调栈)

    题目链接 Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

随机推荐

  1. TCP服务器连接相关知识点

    # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 68 CLOSE_WAIT2 CLOSING136 ESTABLISHED38 FIN_WAIT11 ...

  2. 延时Led灯

    1.代码:#include <reg52.h>typedef unsigned int  u16;typedef unsigned char u8;sbit led = P1^0;void ...

  3. glade2支持C++代码的输出(2)

    今天更新了一个BaseObject的代码:BaseObject.002.zip 同时将glade2生成C++代码的代码进行了调整,基于2.12.2的补丁为:cpp_out_2.patch.tar.xz ...

  4. MSDeploy 同步时不删除原有文件

    在 jenkins里  Execute Windows batch command "C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\m ...

  5. C#------如何获取本机IP地址

    转载: http://www.narkii.com/club/thread-396944-1.html 代码: private string GetIpAddress() { string hostN ...

  6. mysql 字符串

    mysql中一个字符串,既可以用两个单引号表示,也可以用两个双引号表示. 比如字符串 wangxiaowei,用单引号表示 'wangxiaowei',双引号表示"wangxiaowei&q ...

  7. 跟我一起玩转Sencha Touch 移动 WebApp 开发(一)

    1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 自定义图标的方式 WebApp产品测试和发布 HTML5离线缓存 发布 ...

  8. yourphp读取分类名称{$Categorys[$r[catid]]['catname']}

    页面代码:  product_list.html 提供分类的id,找出分类的名称 {$Categorys[$r[catid]]['catname']}

  9. NestIn VS插件 visual studio 中将同类CS文件放在一起显示

    https://visualstudiogallery.msdn.microsoft.com/9d6ef0ce-2bef-4a82-9a84-7718caa5bb45 Nest files in So ...

  10. Orchard源码分析(3):Orchard.WarmupStarter程序集

    概述 Orchard.WarmupStarter程序集包含三个类:WarmupUtility.WarmupHttpModule和Starter<T>.该程序集主要为Orchard应用启动初 ...