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. 【工具】---- json-server基本使用

    一.概念 在开发过程中,前端通常需要等待后端开发完接口后,再调用接口渲染相应的数据,这会影响开发效率.而json-server的作用就是为了解决前后端并行开发的痛点,在本地模拟后端接口用来测试前端效果 ...

  2. MySQL replace into 用法(insert into 的增强版)

    转 http://blog.csdn.net/risingsun001/article/details/38977797 MySQL replace into 用法(insert into 的增强版) ...

  3. Hadoop Hive概念学习系列之hive的正则表达式初步(六)

    说在前面的话 hive的正则表达式,是非常重要!作为大数据开发人员,用好hive,正则表达式,是必须品! Hive中的正则表达式还是很强大的.数据工作者平时也离不开正则表达式.对此,特意做了个hive ...

  4. .Net实战之反射相关类之间的人体经络关系

    --1.类的部分组成成员 --2.巧记成员之间的关系 [MyTable("T_UserInfo")] public class UserInfo : Person, UserSer ...

  5. Python在云端编程之IPython notebook

    Python在云端编程之IPython notebook 如果本地编程考虑到Python版本,机器位数,编译环境,科学栈安装等等繁琐的事,弄得你焦头烂额,不如移步云端,省去这些繁琐过程,在云端编程是很 ...

  6. UVM基础之---------uvm factory机制base

    从名字上面就知道,uvm_factory用来制造uvm_objects和component.在一个仿真过程中,只有一个factory的例化存在. 用户定义的object和component types ...

  7. vuex的各个细节理解(因人而异)

    应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...

  8. tcpdump命令使用方法

    NAME tcpdump - 转储网络上的数据流 总 tcpdump [ -adeflnNOpqStvx ] [ -c count ] [ -F file ] [ -i interface ] [ - ...

  9. c++类简介

    C++类(Class)总结   一.C++类的定义     C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性  protected: // ...

  10. ThinkPHP---layer插件

    [概论] (1)layer是基于jquery开发的一款美化弹框的插件,主要用于弹框效果的交互.但其他功能和组件也日益完善 官网:http://layer.layui.com 在线手册:http://w ...