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] 中任意的一个值. ...
随机推荐
- echo追加字符串到文件末尾
1.覆盖 echo "string" > filename 2.追加 echo "string" >> filename
- TP-LINK配置公网映射
公室里的主机获取到的地址是路由器分配的私网地址,通常是192.168.1.x,只有挂在同一个路由器底下的其它主机可以访问,路由器外面的主机是无法访问的.但是有时候我们希望把办公室内的服务器上的服务暴露 ...
- Anaconda基本用法
Anaconda基本用法 conda info --envs/(-e) // 查看当前的环境变量 conda create -n py36(环境的名称,随意) python=3.6(指定版本) // ...
- java 反射---学习笔记
一.java的动态性 反射机制 动态编译 动态执行JavaScript代码 动态字节码操作 二.动态语言 动态语言 程序运行时,可以改变程序结构或变量类型,典型的语言:python.ruby.java ...
- POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- os系统下安装Python2和Python3
一.下载Xcode工具 1.在App Store 里面下载并安装Xcode 2.安装好Xcode后就打开它,首次进入会有一些LicenceAgreement,点同意就是了,然后就进入到 这个界面: 3 ...
- POJ1222熄灯问题【位运算+枚举】
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14231 Accepted: 8 ...
- C#上位机开发(三)—— 构建SerialAssistant雏形
上一篇简单介绍了C#的一些基本知识,并成功的Hello,World,那么从这篇开始,我们来自己动手写一个串口助手: 1.构思功能 串口助手在单片机开发中经常被用来调试,最基本的功能就是接收功能和发送功 ...
- unity 菜单栏添加新菜单
using UnityEngine; using System.Collections; using UnityEditor; public class jqmTools : CreateSphere ...
- Leetcode 213.大家劫舍II
打家劫舍II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两 ...