HDU 4923 Room and Moor
题目链接: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的更多相关文章
- HDU 4923 Room and Moor(推理+栈维护)
HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...
- HDU 4923 Room and Moor (单调栈)
题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...
- hdu 4923 Room and Moor [ 找规律 + 单调栈 ]
传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- 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. ...
- HDU 4923 Room and Moor(瞎搞题)
瞎搞题啊.找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和. 然后从前向后扫一遍.变扫边进行合并.每次合并.合并的是他的前驱.这样到最后从t-1找出的那条链就是 ...
- hdu 4923 Room and Moor (单调栈+思维)
题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得 sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...
- 【HDU】4923 Room and Moor(2014多校第六场1003)
Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- hdu 4923 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...
- Hdu 4923(单调栈)
题目链接 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
随机推荐
- LightOj1089(求点包含几个线段 + 线段树)
题目链接 题意:n( n <= 50000 ) 个线段,q ( q <= 50000) 个点,问每个点在几个线段上 线段端点的和询问的点的值都很大,所以必须离散化 第一种解法:先把所有的线 ...
- java编程思想-java 异常使用指南
应该在以下情况下使用异常: 在恰当的级别处理问题(在知道该如何处理的情况下才捕获异常). 解决问题并且重新调用产生异常的方法. 进行少许修补,然后绕过异常发生的地方继续执行. 用别的数据进行计算,以代 ...
- BZOJ4690: Never Wait for Weights
裸带权并查集. #include<cstdio> #define N 100005 int m,i,j,s,t,u,d[N],p[N]; char k; int find(int i){ ...
- nodejs fs module
fs.watchFile(filename[, options], listener)# Added in: v0.1.31 filename <String> | <Buffer& ...
- lua 闭包
--匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues function newCounter() return function() i = i + r ...
- 9月26日JavaScript表单验证、正则表达
一.非空验证 trim:去空格(去掉前后的空格),任何字符串都可以用这个方法.写法为:if(v.trim().length==0),表示如果去掉空格后的字符串的长度为0. <body> & ...
- ubuntu下JDK的安装
硬盘上有下载好的JDK,直接解压后配置profile环境变量就行 export JAVA_HOME=/usr/lib/jvm/java-8-oracle export JRE_HOME=${JAVA_ ...
- Sencha Touch 2.2 Store Proxy 异常监控
移动端到服务端通信往往会发生很多莫名的异常情况,如何有效的监控proxy异常,给用户友好的用户体验呢? Proxy给我提供了异常exception的监听事件,只需要监控该项目即可. Sencha To ...
- RHEL提示RHN没有注册问题的解决方法
1.系统RHEL5.5,初次使用yum时出现以下问题: [root@localhost real]# yum update Loaded plugins: rhnplugin, security Th ...
- Autofac.Integration.Mvc.Owin分析
using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...