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. 手机访问PC网站自动跳转到手机网站代码

    方法一: <script type="text/javascript"> try { var urlhash = window.location.hash; if (! ...

  2. java虚拟机全集(31篇文章)

    深入理解java虚拟机系列 深入理解Java虚拟机笔记---内存区域 深入理解Java虚拟机笔记---判断对象是否存活 深入理解Java虚拟机笔记---垃圾收集算法 深入理解Java虚拟机笔记---垃 ...

  3. 51nod 1029 大数除法

    1029 大数除法 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出2个大整数A,B,计算A / B和A Mod B的结果. Input 第1 ...

  4. O - Combinations (组合数学)

    Description Computing the exact number of ways that N things can be taken M at a time can be a great ...

  5. CSS之背景设置、字体设置、文本设置

    <html> <head> <meta charset="utf-8"> <title>单行文本框与多行文本框</title& ...

  6. UML中类之间的关系

    UML中类之间的关系分为以下几种:依赖.关联.泛化.聚合.组合. 依赖是指一个类使用了另一个类,它是一种使用关系,描述了一个事物的规格说明的变化可能会影响到使用它的另一个事物(反之不一定).最常见的依 ...

  7. 309 Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i.Design an alg ...

  8. SqlServer数据库(可疑)解决办法

    -- 当数据库发生这种操作故障时,可以按如下操作步骤可解决此方法,打开数据库里的Sql 查询编辑器窗口,运行以下的命令. --1.修改数据库为紧急模式 ALTER DATABASE Zhangxing ...

  9. 26 c#类的组合

    组合即将各个部分组合在一起.程序设计中就是用已有类的对象来产生新的类. 桌子由木板和钉子组合而成,台灯使用灯座,灯管,电线,接头等拼起来的.我们发现自己周围的很多东西都是由更小的其它东西拼凑构成的,就 ...

  10. PyCharm使用指南及更改Python pip源为国内豆瓣

    PyCharm基本使用 1.在PyCharm下为python项目配置python本地解释器 setting-->Project:pycharm workspace-->Project In ...