HDU 4923 Room and Moor(瞎搞题)
瞎搞题啊。找出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
which satisfies that:
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.
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
1.428571
1.000000
0.000000
0.000000
#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(瞎搞题)的更多相关文章
- B. Salty Fish Go! -期望题(瞎搞题)
链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...
- 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 (多校第六场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 [ 找规律 + 单调栈 ]
传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 4923 Room and Moor
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1. ...
- hdu 4923 Room and Moor (单调栈+思维)
题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得 sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...
- 牛客练习赛22 简单瞎搞题(bitset优化dp)
一共有 n个数,第 i 个数是 xi xi 可以取 [li , ri] 中任意的一个值. 设 ,求 S 种类数. 输入描述: 第一行一个数 n. 然后 n 行,每行两个数表示 li,ri. 输出 ...
- 简单瞎搞题(bitset的操作)
链接:https://www.nowcoder.com/acm/contest/132/C来源:牛客网 题目 一共有 n个数,第 i 个数是 xi xi 可以取 [li , ri] 中任意的一个值. ...
随机推荐
- android studio 调试安装
最新的手机无法debug安装调试. vivo x21 oppo r11等 解决办法: 1,关闭instant run Android Studio->Preferences->Build ...
- Webpack 入门(一):安装 / 打包 / 命令行
一:安装webpack和基本环境搭建 新建一个工作的文件夹(我取的名字叫Webpack) 打开命令行,cd进入该文件夹 //初始化一下npm > E:\work\Webpack>npm i ...
- PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (三) -- Header Exchange
此模式下,消息的routing key 和队列的 routing key 会被完全忽略,而是在交换机推送消息和队列绑定交换机时, 分别为消息和队列设置 headers 属性, 通过匹配消息和队列的 h ...
- python send email
#!/usr/bin/python # -*- coding: UTF-8 -*- # coding:utf8 from smtplib import SMTP_SSL from email.head ...
- vue中去掉烦人的格式警告(eslint )
解决办法: 一,源头上解决,下次创建项目时就不要使用eslint连接项目代码 如上图所示,就是在这一步的时候选择no: 二,在build文件夹中找到webpack.base.conf.js文件 找到右 ...
- [GXOI/GZOI2019]与或和(单调栈)
想了想决定把这几题也随便水个解题报告... bzoj luogu 思路: 首先肯定得拆成二进制30位啊 此后每一位的就是个01矩阵 Q1就是全是1的矩阵个数 Q2就是总矩阵个数减去全是0的矩阵个数 ...
- Python之模块和包的创建与使用
一.模块的概念 在计算机的开发过程中,随着程序代码越写越多,在一个文件里代码就越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,放在不同的文件里面,这样,每个文件包含的代码就相对 ...
- phtyon,通过while循环简单的用户名和密码登录
_username='zhangxin' _password='abc123' _username1='zhaopeng' _password1='abc1234' _username2=" ...
- Vue如何使用vue-area-linkage实现地址三级联动效果
很多时候我们需要使用地址三级联动,即省市区三级联动.网上有很多插件,在此介绍Vue的一款地区联动插件:vue-area-linkage,下面介绍如何使用这个插件实现地址联动效果: 1. ...
- Linux备份-删除指定日期内文件
#!/usr/bin/env bash source /etc/profile echo " *************** start filter *************** &q ...