题目链接

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 842    Accepted Submission(s): 250

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

 Accepted Code:

 /*************************************************************************
> File Name: 4923.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月08日 星期五 22时27分38秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const double eps = 1e-;
const int maxn = ;
int n;
int a[maxn], st[maxn]; struct node {
double x; //区间均值
int l, r, one; //区间范围及1的个数
}A[maxn]; // unite i to j
void unite(int i, int j) {
A[j].l = A[i].l;
A[j].x = A[j].one + A[i].one + 0.0;
A[j].x /= A[j].r - A[j].l + ;
A[j].one = A[i].one + A[j].one;
} double solve() {
int len = ;
for (int i = ; i <= n; i++) {
A[len].x = a[i] + 0.0;
A[len].l = i;
i++;
while (i <= n && a[i] == a[i-]) i++;
A[len].r = i - ;
i--;
if (a[i]) A[len].one = A[len].r - A[len].l + ;
else A[len].one = ;
len++;
}
int top = ;
for (int i = ; i < len; i++) {
while (top && A[i].x < A[st[top-]].x) {
unite(st[top-], i);
top--;
}
st[top++] = i;
}
double ans = 0.0;
for (int i = ; i < top; i++) {
for (int j = A[st[i]].l; j <= A[st[i]].r; j++) {
ans += (a[j] - A[st[i]].x) * (a[j] - A[st[i]].x);
}
}
return ans + eps;
} int main(void) {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", a + i);
printf("%.6f\n", solve());
}
return ;
}

最新文章

  1. python 中的decorator
  2. JS 跨源请求
  3. [转]crontab命令指南
  4. Android 6 Marshmallow USB调试授权
  5. 获取月份对应的day
  6. CSS基础(02)
  7. 线性代数(矩阵乘法):NOI 2007 生成树计数
  8. 《python基础教程》笔记之 元组
  9. java中的基本数据类型的转换
  10. CSS中层叠和CSS的7阶层叠水平(上篇)
  11. BZOJ1131 [POI2008]Sta 其他
  12. WDTP注册破解
  13. 亲测GO环境搭建,理解go build、go install、go get
  14. Java知多少(85)文本框和文本区
  15. 师弟推荐软件-/mathpix
  16. 数字IC设计入门书单
  17. Kafka 0.8翻译官网精华.md
  18. poj3304 Segments【计算几何】
  19. Densenet 相关
  20. sql 随机取数

热门文章

  1. PAT甲级——A1076 Forwards on Weibo
  2. SpringMVC处理请求的大致流程是怎么样的
  3. JDK1.8 之Lambda表达式
  4. Python学习day19-常用模块之re模块
  5. matlab 实现感知机线性二分类算法(Perceptron)
  6. Luogu P3558 [POI2013]BAJ-Bytecomputer(线性dp)
  7. phpmyadmin连接远程mysql
  8. sql 2000 or sql2005 数据库日志删除
  9. git 创建.gitignore忽略不必要的文件
  10. 安装配置flask环境