Hdu 4923(单调栈)
2024-09-06 13:44:33
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 842 Accepted Submission(s): 250Problem DescriptionPM 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:InputThe 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.OutputOutput the minimal f (A, B) when B is optimal and round it to 6 decimals.Sample Input4
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 1Sample Output1.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 ;
}
最新文章
- python 中的decorator
- JS 跨源请求
- [转]crontab命令指南
- Android 6 Marshmallow USB调试授权
- 获取月份对应的day
- CSS基础(02)
- 线性代数(矩阵乘法):NOI 2007 生成树计数
- 《python基础教程》笔记之 元组
- java中的基本数据类型的转换
- CSS中层叠和CSS的7阶层叠水平(上篇)
- BZOJ1131 [POI2008]Sta 其他
- WDTP注册破解
- 亲测GO环境搭建,理解go build、go install、go get
- Java知多少(85)文本框和文本区
- 师弟推荐软件-/mathpix
- 数字IC设计入门书单
- Kafka 0.8翻译官网精华.md
- poj3304 Segments【计算几何】
- Densenet 相关
- sql 随机取数