hdu 4923 Room and Moor
来源:互联网 发布:淘宝跨店凑单什么意思 编辑:程序博客网 时间:2024/05/17 09:24
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 446 Accepted Submission(s): 125
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.
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.
#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<list>#include<map>#include<set>using namespace std;const double eps=1e-9;int n;int a[100010];struct node{ int cnt; int all; double b; node(int cnt=0,int all=0,double b=0):cnt(cnt),all(all),b(b){}};vector<node>cur;node res[100010];void deal_pre(){ cur.clear(); while(n>0 && a[n]==1) n--; for(int i = 1; i <= n; i++){ if(a[i] == 1){ int j = i; int cnt = 0; while(j<=n && a[j]==1) j++,cnt++; while(j<=n && a[j]==0) j++; double b = cnt * 1.0 / (j-i); cur.push_back(node(cnt, j-i, b)); i=j-1; } }}int sign(double x){ if(x > eps) return 1; else if(x < -eps) return -1; return 0;}//int binary(int ed, double x){// int st = 0, mid ;// while(st + 1 < ed){// mid = ( st + ed ) >> 1;// if( sign( x - res[mid].b )>= 0 ) st = mid;// else ed = mid;// }// if( sign( x - res[st].b) == 0 ) st++;// return st;//}int deal_res(int len){ int i = 0; res[0] = cur[i++]; int ed = 0; while(i < len){ if(sign( cur[i].b - res[ed].b ) >= 0){ res[++ed] = cur[i]; } else{// int st = binary(ed,cur[i].b);// for(int iy = st; iy <= ed; iy++){// sum += res[iy].all;// cnt += res[iy].cnt;// }// ed = st; int sum = cur[i].all; int cnt = cur[i].cnt; while(ed >= 0){ sum += res[ed].all; cnt += res[ed].cnt; double tp = 1.0 * cnt / sum; if( ed == 0){ res[ed]= node(cnt,sum,tp); break; } else{ if(tp >= res[ed-1].b){ res[ed] = node(cnt , sum, tp); break; } else ed--; } } } i++; } return ed;}double work(){ int len = cur.size(); if(len == 0) return 0; int ed = deal_res(len); double ans = 0; for(int i = 0; i <= ed; i++){ int all = res[i].all; int cnt = res[i].cnt; double b1 = (all - cnt) * 1.0 / all; double b2 = cnt * 1.0 / all; ans = ans + cnt * b1 * b1 + (all - cnt) * b2 * b2; } return ans;}int main(){//freopen("in.in","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); deal_pre(); printf("%.6f\n",work()); }return 0;}
0 0
- HDU 4923 Room and Moor
- HDU 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- HDU-4923-Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- hdu 4923 Room and Moor
- Hdu 4923 Room and Moor
- HDU 4923 Room and Moor
- hdu 4923 Room and Moor(线性表)
- hdu 4923 Room and Moor 堆栈
- HDU 4923 Room and Moor(瞎搞题)
- hdu 4923 Room and Moor 贪心+YY
- HDU 4923 Room and Moor【栈】【想法】
- HDU 4923 - Room and Moor (贪心)
- HDU 4923 Room and Moor 贪心+栈
- 监听系统开机广播并启动Activity
- grtgrtghrt
- 支付宝服务窗 开发 验证
- form不提交问题
- 难道他能通过电话
- hdu 4923 Room and Moor
- SOA、网格计算、云计算与P2P技术
- 7.23构造函数
- CSDN积分排名记录
- 【Android开发-5】界面装修,五大布局你选谁
- hive基本命令
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- BOJ 480 田田背课文