hdu4923(2014多校第六场第三题)
来源:互联网 发布:布兰诗歌 知乎 编辑:程序博客网 时间:2024/06/15 05:02
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 503 Accepted Submission(s): 138
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:
![](http://acm.hdu.edu.cn/data/images/C507-1003-1.jpg)
![](http://acm.hdu.edu.cn/data/images/C507-1003-1.jpg)
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.
Sample Input
491 1 1 1 1 0 0 1 191 1 0 0 1 1 1 1 140 0 1 140 1 1 1
Sample Output
1.4285711.0000000.0000000.000000
贪心,注意一堆0前面的一堆1的x值一定相等,然后求出每一堆的x,如果前面x大于现在的,就把前面的和现在的合并,一直贪心下去即可。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int u[100005],x[100005],y[100005];double a[100005],b[100005];int tear;int main(){ int i,n,t,x1,y1; scanf("%d",&t); while(t--){ scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&u[i]); } tear=0; i=1; while(i<=n){ x[tear]=0; y[tear]=0; while(i<=n&&u[i]==1){ x[tear]++; i++; } while(i<=n&&u[i]==0){ y[tear]++; i++; } a[tear]=(double)(x[tear])/(y[tear]+x[tear]); b[tear]=x[tear]*(1-a[tear])*(1-a[tear])+y[tear]*a[tear]*a[tear]; while(tear>=1){ if(a[tear-1]>a[tear]){ x1=x[tear-1]+x[tear]; y1=y[tear-1]+y[tear]; a[tear-1]=(double)x1/(x1+y1); b[tear-1]=x1*(1-a[tear-1])*(1-a[tear-1])+y1*a[tear-1]*a[tear-1]; x[tear-1]=x1; y[tear-1]=y1; tear--; } else break; } tear++; } double s=0; for(i=0;i<tear;i++){ s+=b[i]; } printf("%.6lf\n",s); } return 0;}
0 0
- hdu4923(2014多校第六场第三题)
- hdu4923(想法题)
- 2014多校联合-第六场
- HDU5353.Average(多校第六场)
- 多校练习第六场
- 多校第三场
- 多校第三场
- 2014多校联合-第三场
- hdu5762(2016多校第三场,思维题)
- hdu 4925 Apple Tree 2014多校第六场 贪心
- 新生多校第六场 插塔憋憋乐(数论+贪心)
- 多校联赛 第三场
- HDU 多校第三场
- 2013 多校第三场
- 多校第三场总结
- 多校联合第三场
- 多校联赛第三场
- HDU 4923 Room and Moor (多校第六场C题) 单调栈
- 正则表达式截取URL参数
- 你不是正站在快乐
- ios视频学习3.2 类的声明和对象的创建—内存分析1
- JqueryMobile为Listview动态添加、删除查询功能
- 看就看就看好即可很快就回家尽快尽快
- hdu4923(2014多校第六场第三题)
- LeetCode:Sort List (Java)
- [笔试题] 两个有趣的问题
- el表达式判断是否为空
- sscanf函数用法
- hdu 4586——Play the Dice
- FPC的编译宏
- 【Django】创建模板
- poj1609 Tiling Up Blocks