HDU 4923
来源:互联网 发布:featutelayer json 编辑:程序博客网 时间:2024/06/06 00:19
去掉前导0和后导1
之后切分成1111100,11100,10连续的一段,每段算一个平均值合并
#include <cstdio>#include <cstring>#include <stack>using namespace std;const int maxn = 100007;const double eps = 1e-8;int num[maxn];struct Seg { int l, r, zero, one;};int tot_s;struct Seg s[maxn];stack<struct Seg> sta;int sgn(double x) { return x < -eps ? -1 : x > eps;}double cal(struct Seg a) { double ans = a.one + a.zero; ans = a.one / ans; return ans;}int main() {// freopen("in.txt" , "r", stdin); int n; int test_case; scanf("%d", &test_case); while (test_case--) { scanf("%d", &n); double ans = 0; int loc_first = 0; int loc_last = n + 1; tot_s = 0; for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); } for (int i = 1; i <= n && num[i] == 0; i++) { loc_first = i; } for (int j = n; j >= 1 && num[j] == 1; j--) { loc_last = j; } loc_last--; loc_first++; num[n + 1] = 1; if (loc_first > loc_last) { printf("%.6f\n", ans); continue; } while (loc_first <= loc_last) { s[tot_s].l = loc_first; while (num[loc_first] == 1) { loc_first++; } s[tot_s].one = loc_first - s[tot_s].l; while (num[loc_first] == 0) { loc_first++; } s[tot_s].r = loc_first - 1; s[tot_s].zero = s[tot_s].r - s[tot_s].l + 1 - s[tot_s].one; tot_s++; }// for (int i = 0; i < tot_s; i++) {// printf("%d %d\n", s[i].l, s[i].r);// } sta.push(s[0]); for (int i = 1; i < tot_s; i++) { struct Seg cnt = s[i]; while (!sta.empty()) { double pre_ave = cal(sta.top()); double cnt_ave = cal(cnt); if (sgn(cnt_ave - pre_ave) >= 0) { sta.push(cnt); break; } struct Seg pre_seg = sta.top(); sta.pop(); cnt.l = pre_seg.l; cnt.one += pre_seg.one; cnt.zero += pre_seg.zero; } if (sta.empty()) { sta.push(cnt); } } while (!sta.empty()) { struct Seg cnt = sta.top(); double ave = cal(cnt); ans += (1 - ave) * (1 - ave) * cnt.one + ave * ave * cnt.zero; sta.pop(); } printf("%.6f\n", ans); } return 0;}
0 0
- HDU 4923
- HDU 4923
- hdu 4923
- hdu 4923 第六次多校
- HDU 4923 series1
- hdu 4923 单调栈
- 2014多校联合六(HDU 4923 HDU 4925 HDU 4927 HDU 4930)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- tcp wrapper
- 数据库巡检脚本
- redhat linux 5.6 安装oracle 11gR2客户端
- 22 PPP协议
- Java 面向对象 面试题
- HDU 4923
- 通过log4j保存异常信息
- JPA 注解以及pojo转为表 2
- unity3d之帧动画
- 创建SSL证书,配置IPython notebook服务器
- HDU 4930
- 第二届360杯全国大学生信息安全技术大赛部分解题思路(加密解密题)
- POJ 3356 AGTC
- GGDB常用代码