瞎搞题啊。找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和。

然后从前向后扫一遍。变扫边进行合并。每次合并。合并的是他的前驱。这样到最后从t-1找出的那条链就是最后满足条件的数的大小。

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 307    Accepted Submission(s): 90

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
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)? 0:x) using namespace std; const int maxn = 1000010;
int num[maxn];
int sum[maxn][2];
int pre[maxn];
double x[maxn]; int main()
{
int T;
cin >>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++) scanf("%d",&num[i]);
int t = 0;
int cnt1 = 0;
int cnt2 = 0;
if(!num[0]) cnt1 = 1;
if(num[0]) cnt2 = 1;
for(int i = 1; i < n; i++)
{
if(num[i] > num[i-1])
{
sum[t][0] = cnt1;
sum[t++][1] = cnt2;
cnt1 = cnt2 = 0;
if(!num[i]) cnt1++;
if(num[i]) cnt2++;
continue;
}
if(!num[i]) cnt1++;
if(num[i]) cnt2++;
}
sum[t][0] = cnt1;
sum[t][1] = cnt2;
t++;
for(int i = 0 ; i < t; i++) x[i] = (1.0*sum[i][1]/((sum[i][0]+sum[i][1])*1.0));
pre[0] = -1;
for(int i = 1; i < t; i++)
{
if(x[i] < x[i-1])
{
sum[i][0] += sum[i-1][0];
sum[i][1] += sum[i-1][1];
x[i] = 1.0*sum[i][1]/(sum[i][1]+sum[i][0])*1.0;
pre[i] = pre[i-1];
int p = pre[i];
while(p != -1)
{
if(x[i] < x[p])
{
sum[i][0] += sum[p][0];
sum[i][1] += sum[p][1];
x[i] = 1.0*sum[i][1]/(sum[i][0]+sum[i][1])*1.0;
pre[i] = pre[p];
p = pre[p];
continue;
}
break;
}
continue;
}
pre[i] = i-1;
}
int p = pre[t-1];
double ans =0;
ans += sum[t-1][0]*pow(x[t-1], 2)+sum[t-1][1]*pow(x[t-1]-1, 2);
while(p != -1)
{
ans += sum[p][0]*pow(x[p], 2)+sum[p][1]*pow(x[p]-1, 2);
p = pre[p];
}
printf("%.6lf\n",ans);
}
return 0;
}

HDU 4923 Room and Moor(瞎搞题)的更多相关文章

  1. B. Salty Fish Go! -期望题(瞎搞题)

    链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...

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

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

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

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

  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 [ 找规律 + 单调栈 ]

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

  6. HDU 4923 Room and Moor

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1.     ...

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

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

  8. 牛客练习赛22 简单瞎搞题(bitset优化dp)

    一共有 n个数,第 i 个数是 xi  xi 可以取 [li , ri] 中任意的一个值. 设 ,求 S 种类数. 输入描述: 第一行一个数 n. 然后 n 行,每行两个数表示 li,ri.   输出 ...

  9. 简单瞎搞题(bitset的操作)

    链接:https://www.nowcoder.com/acm/contest/132/C来源:牛客网 题目 一共有 n个数,第 i 个数是 xi  xi 可以取 [li , ri] 中任意的一个值. ...

随机推荐

  1. Codeforces_789C_(dp)

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. freopen()重定向的打开和关闭

    freopen函数 功能 使用不同的文件或模式重新打开流,即重定向. 实现重定向,把预定义的标准流文件定向到由path指定的文件中.(直观感觉/实际操作都像是把文件定向到流,难道是说,对流来说就是重定 ...

  3. sehlle脚本获取linux服务器基本信息

    将以下代码全选复制在linux机器上新建x.sh文件编辑复制进去执行即可. #获取linux服务器基本信息脚本 #!/bin/bash # #Name:system_info #Ver:1.0 #Au ...

  4. java_线程的通信

    线程的通信共有三个方法: wait()运行时阻塞,释放锁 notify()唤醒阻塞线程 notifll()唤醒全部阻塞线程 public class ThreadTest01 { public sta ...

  5. JS正则表达式验证(一)

    目录: 手机号验证 固定电话验证 手机号验证: 写法[1]--->!(/^1[34578]\d{9}$/.test(phone)):以1开头,第二位可能是3/4/5/7/8等的任意一个,在加上后 ...

  6. 第1节 MapReduce入门:11、mapreduce程序的入门-2

    1.5.WordCount示例编写 1.JobMain.java类 package cn.itcast.wordcount; import org.apache.hadoop.conf.Configu ...

  7. NET VBCSCompiler.exe占用100%,造成项目卡顿的的解决方法

    1)服务器环境 最低配 的window server 2008 r2, 配置低容易发现问题‘ 2)事件描述 :项目打开缓慢,查询列表卡顿 3)问题分析:排除代码问题, ->打开服务器任务管理器 ...

  8. 在springBoot的控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: properties形式 logging.level.com.eth.wallet.mapp ...

  9. 基础知识:if条件、while循环、for循环 相关练习

    1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! while True: name = input('请输入用户名:') psw = inpu ...

  10. MySQL Connector/Python 接口 (三)

    本文参见这里. 使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15% from __future__ import print_function from dec ...