hdu 4923 Room and Moor--2014 Multi-University Training Contest 6
来源:互联网 发布:网络棋牌游戏开发教程 编辑:程序博客网 时间:2024/06/05 00:45
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 742 Accepted Submission(s): 220
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.
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
Author
BUPT
Source
2014 Multi-University Training Contest 6
Recommend
We have carefully selected several similar problems for you: 4929 4928 4926 4924 4922
这道题,比赛时候没做出来,后面看了题解又想了好久才搞出来的。
对f(x)求导可以得到 在一段区间内,当x为平均值时,f(x)的值最小,但由于我们可以分成好多段,每一段的值可以是不同的。因此我们需要模拟一个栈来维护它。
从左到右进行扫描,遇到A[i],时把它加入栈内。然后检查栈顶是否有x[i]>x[i-1],若有则将其合并取平均值,为了方面我这边用num[i]表示第i个区间内1的数量,len[i]表示第i个段的长度。所以只需要判断 num[i]*len[i-1]>num[i-1]*len[i]的大小即可。
到最后,我们加上每个段的大小即可。需要注意的一点是num[i]统计的是第i段1的数量,那么len[i]-num[i] 统计的便是该段0的数量。将它们加起来即可。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<string>#include<vector>#include<map>#include<set>#include<cstdlib>#include<stack>#include<queue>#include<bitset>using namespace std;#define CLR(A) memset(A,0,sizeof(A))const int MAX=100010;double num[MAX];double len[MAX];int main(){ int T; while(~scanf("%d",&T)){ while(T--){ int n,cnt=0; CLR(num);CLR(len); scanf("%d",&n); for(int i=0;i<n;i++){ int v;scanf("%d",&v); num[cnt]=v; len[cnt]=1; while(cnt>=1){ if(num[cnt]*len[cnt-1]>num[cnt-1]*len[cnt]) break; num[cnt-1]+=num[cnt]; len[cnt-1]+=len[cnt]; cnt--; } cnt++; } double ret=0.0; for(int i=0;i<cnt;i++){ double x=num[i]/len[i]; ret+=x*x*(len[i]-num[i])+(1-x)*(1-x)*num[i]; } printf("%.6lf\n",ret); } } return 0;}
0 0
- hdu 4923 Room and Moor--2014 Multi-University Training Contest 6
- hdu 4923 Room and Moor (2014 Multi-University Training Contest 6)
- 【2014 Multi-University Training Contest 6】 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 4925 Apple Tree--2014 Multi-University Training Contest 6
- hdu 4927 Series 1--2014 Multi-University Training Contest 6
- hdu 4925 Apple Tree 2014 Multi-University Training Contest 6
- 2014 Multi-University Training Contest 6小记
- echarts入门教程
- 隐式意图的配置 →显示意图激活另外一个activity
- 辛星也谈vim与emacs
- 【note】SSC配置工具Slave STack Code Tool操作重点8月8
- 如何发送 USB 控制传输
- hdu 4923 Room and Moor--2014 Multi-University Training Contest 6
- 管道 |wc -l (wc也是一个命令)
- 后果将空间看钢结构看风景合法合规
- Android逆向分析之静态分析工具篇
- 设置ImageView圆角及边框问题
- UVA 11330 - Andy's Shoes(置换分解)
- 对MarshalByRefObject的解释
- Java的JDBC事务详解
- linux-多线程