Room and Moor

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.

 

Output
Output the minimal f (A, B) when B is optimal and round it to 6 decimals.
 

Sample Input
4 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 1
 

Sample Output
1.428571 1.000000 0.000000 0.000000

题意:不多说了。

sl: 其实就是一个贪心的思想。

网上讲解很多。比赛时没搞出来。。。呵呵了。

 1 // by caonima
 2 // hehe
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <stack>
 6 #include <queue>
 7 #include <vector>
 8 #include <algorithm>
 9 using namespace std;
 const int MAX = 1e6+;
 struct node {
     int one,zero;
     double val;
 }v[MAX];
 stack<node> ans;
 int a[MAX];
 void init() {
     while(!ans.empty()) ans.pop();
 }
 int main() {
     int cas,n;
     scanf("%d",&cas);
     while(cas--) {
         scanf("%d",&n);
         for(int i=;i<=n;i++) {
             scanf("%d",&a[i]);
         }
         a[n+]=;
         int L=,R=n;
         while(a[L]==) L++;
         while(a[R]==) R--;
         if(L>=R) printf("0.000000\n");
         else {
             int id=;
             for(int i=L;i<=R;) {
                 int cnt0=,cnt1=;
                 while(a[i]==) {
                     cnt1++; i++;
                 }
                 while(a[i]==) {
                     cnt0++; i++;
                 }
                 v[id].one=cnt1; v[id].zero=cnt0;
                 v[id++].val=(double) cnt1/(double)(cnt0+cnt1);
             }
             init();
             for(int i=;i<id;i++) {
 
                 if(ans.empty()) ans.push(v[i]);
                 else {
                     node vn=ans.top();
                     if(vn.val<=v[i].val) ans.push(v[i]);
                     else {
                         node t=v[i];
                         while(true) {
                             node vx=ans.top();
                             if(vx.val>t.val) {
                                 t.one+=vx.one; t.zero+=vx.zero;
                                 t.val=(double) t.one/(double)(t.one+t.zero);
                                 ans.pop();
                             }
                             else {
                                 ans.push(t);
                                 break;
                             }
                             if(ans.empty()) {
                                 ans.push(t);
                                 break;
                             }
                         }
                     }
                 }
             }
             double res=;
             while(!ans.empty()) {
                 node vn=ans.top();
                 ans.pop();
                 res+=vn.val*vn.val*vn.zero+(-vn.val)*(-vn.val)*vn.one;
             }
             printf("%.6lf\n",res);
         }
 
     }

84 }

HDU 4923 (贪心+证明)的更多相关文章

  1. HDU 4923 Room and Moor (单调栈)

    题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...

  2. HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)

    A.GPA(HDU4802): 给你一些字符串对应的权重,求加权平均,如果是N,P不计入统计 GPA Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. HDU 4923 Room and Moor

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1.     ...

  4. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  5. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  6. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  7. hdu 4923 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...

  8. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  9. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

随机推荐

  1. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑【矩阵乘法+Floyd】

    唔不知道怎么说--大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神 首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf 然后注 ...

  2. bzoj题目大体分类

    http://m.blog.csdn.net/article/details?id=51387623

  3. python re的使用

    re 正则表达式操作  本模块提供了类似于Perl的正则表达式匹配操作.要匹配的模式和字符串可以是Unicode字符串以及8位字符串. 正则表达式使用反斜杠字符('\')来表示特殊的形式或者来允许使用 ...

  4. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

  5. SQL中CRUD C——create 添加数据 R——read 读取数据 U——update 修改数据 D——delete 删除数据

    在SQL server中对数据库的操作: 删除表:drop table 表名修改表:alter table 表名 添加列add 列名 列类型alter table 表名 drop column 列名 ...

  6. 6.12---Swagger中paramType---swagger的RequestParam和ApiImpliciParam----Example中方法带有selective

    paramType:表示参数放在哪个地方    header-->请求参数的获取:@RequestHeader(代码中接收注解)    query-->请求参数的获取:@RequestPa ...

  7. 解决FormClosing事件点击关闭2次的问题

    以下代码:提示框会跳出2遍  private void mFrmmain_FormClosing(object sender, FormClosingEventArgs e) { if (Dialog ...

  8. SQL server 上机练习题

    首先创建一个数据库,里面有 登录表 学生表   课程表   选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...

  9. Quartus 12的TimeQuest Timing Analyzer

    Quartus 12的TimeQuest Timing Analyzer 1.在Quartus II软件打开和设置设计 安装路径下\qdesigns\fir_fliter文件.在Processing ...

  10. 梦想CAD控件安卓参数绘图

    在CAD绘图中,参数化绘图可以帮助我们极大缩短设计时间,用户可以按照设计意图控制绘图对象,这样即使对象发生了变化,具体的关系和测量数据仍将保持不变,能够对几何图形和标注进行控制,可以帮助用户应对耗时的 ...