Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 263    Accepted Submission(s): 73

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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  4930 4929 4928 4927 4926 
 
题意:很容易就读懂了。
题解:首先去掉前导零和最后的1,相当于把整个序列分成几个区间,每部分以1开头,0结尾,即如1 0   1 1 0 0等,可知对于每一个区间,要取得最小值,那这个部分所有的值即对应的这个区间内的平均数,如果这个平均数和前面一个区间的相比较大,就压入栈,否则将栈里的元素顶出,并与当前区间合并求平均数……知道比前面的大为止,最后求出每个区间的对应的Seg(ai - bi)^2 就可以了。至于为什么。。。。说实话全是YY,居然A掉了。。
 
AC代码如下:
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. #define eps 0.00000001
  7. const int LEN = ;
  8.  
  9. int arr[LEN];
  10. struct line
  11. {
  12. int l, r, sum;
  13. double rate;
  14. };
  15.  
  16. stack<line> s;
  17.  
  18. int main()
  19. {
  20. int T, n;
  21. line tmp;
  22. scanf("%d", &T);
  23. while(T--){
  24. scanf("%d", &n);
  25. for(int i = ; i < n; i++)
  26. scanf("%d", arr+i);
  27. int h = ;
  28. while(arr[h] == )
  29. h++;
  30. int k = n - ;
  31. while(arr[k] == )
  32. k--;
  33. for(int i = h; i <= k; i++){
  34. if (i == h || i > h && arr[i-] == && arr[i] == ){
  35. tmp.l = i;
  36. tmp.sum = ;
  37. }
  38. if (i < k && arr[i] == && arr[i+] == || i == k){
  39. tmp.r = i;
  40. //printf("l = %d, r = %d\n", tmp.l, tmp.r);
  41. tmp.rate = tmp.sum * 1.0 / ((tmp.r - tmp.l + ) * 1.0);
  42. //printf("rate=%f\n", tmp.rate);
  43. while(true){
  44. if (s.empty() || s.top().rate - tmp.rate < eps){
  45. s.push(tmp);
  46. break;
  47. }
  48. if (s.top().rate - tmp.rate > eps){
  49. tmp.l = s.top().l;
  50. tmp.sum += s.top().sum;
  51. tmp.rate = tmp.sum*1.0 / ((tmp.r - tmp.l + )*1.0);
  52. s.pop();
  53. }
  54. }
  55. }
  56. if (arr[i] == )
  57. tmp.sum++;
  58. }
  59. double ans = ;
  60. while(!s.empty()){
  61. ans += (( - s.top().rate) * ( - s.top().rate) * s.top().sum + s.top().rate * s.top().rate * (s.top().r - s.top().l + - s.top().sum));
  62. s.pop();
  63. }
  64. printf("%f\n", ans);
  65. }
  66. return ;
  67. }

【HDU】4923 Room and Moor(2014多校第六场1003)的更多相关文章

  1. HDU 4923 Room and Moor (多校第六场C题) 单调栈

    Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. ...

  2. 2014多校第七场1003 || HDU 4937 Lucky Number

    题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数 ...

  3. 2014多校第六场 1010 || HDU 4930 Fighting the Landlords (模拟)

    题目链接 题意 : 玩斗地主,出一把,只要你这一把对方要不了或者你出这一把之后手里没牌了就算你赢. 思路 : 一开始看了第一段以为要出很多次,实际上只问了第一次你能不能赢或者能不能把牌出尽. #inc ...

  4. 2014多校第六场 1007 || HDU 4927 Series 1(杨辉三角组合数)

    题目链接 题意 : n个数,每操作一次就变成n-1个数,最后变成一个数,输出这个数,操作是指后一个数减前一个数得到的数写下来. 思路 : 找出几个数,算得时候先不要算出来,用式子代替,例如: 1 2 ...

  5. 2014多校第六场 1005 || HDU 4925 Apple Tree

    题目链接 题意 : 给你一块n×m的矩阵,每一个格子可以施肥或者是种苹果,种一颗苹果可以得到一个苹果,但是如果你在一个格子上施了肥,那么所有与该格子相邻(指上下左右)的有苹果树的地方最后得到的苹果是两 ...

  6. HDU 4869 Turn the pokers (2014 多校联合第一场 I)

    HDOJ--4869--Turn the pokers[组合数学+快速幂] 题意:有m张扑克,开始时全部正面朝下,你可以翻n次牌,每次可以翻xi张,翻拍规则就是正面朝下变背面朝下,反之亦然,问经过n次 ...

  7. HDU 4865 Peter's Hobby(2014 多校联合第一场 E)(概率dp)

    题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2 ...

  8. 多校第六场 1003 hdu 5355 Cake(贪心)

    题目链接:(数据加强后wa了) hdu 5355 题目大意: 给出一个蛋糕.切成1~n大小的n块.问是否能在不继续分割的情况下拼凑出m等份. 题目分析: 首先我们是可以知道每份蛋糕的尺寸的,利用n*( ...

  9. HDU 4923 Room and Moor(推理+栈维护)

    HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...

随机推荐

  1. swift笔记06

    for in循环 for 被乘数 in 1...5{ println("\(被乘数) 乘以 5 等于 \( 被乘数 * 5)"); } let  女神们 = ["小林&q ...

  2. activity 的返回按钮

    http://www.2cto.com/kf/201210/160251.html 连续点击两次程序就退出程序,这是一个很有趣的程序功能,下来介绍一下我的实现方式(欢迎大家拍砖指点):   1.在Ac ...

  3. DataTable AsEnumerable 的使用

    var p = DataTable.AsEnumerable().Where(t => t.Field<int>("ChannelID") == int.Pars ...

  4. MEMS陀螺仪(gyroscope)的工作原理

    传统的陀螺仪主要是利用角动量守恒原理,因此它主要是一个不停转动的物体,它的转轴指向不随承载它的支架的旋转而变化. 但是MEMS陀螺仪(gyroscope)的工作原理不是这样的,因为要用微机械技术在硅片 ...

  5. Windows服务安装完成后自动启动

    public ServiceInstaller() { //... Installer code here this.AfterInstall += new InstallEventHandler(S ...

  6. VC++自绘界面

    // MySkinDlg.cpp : implementation file // #include "stdafx.h" #include "MySkin.h" ...

  7. alternaiate terms

    操作系统就是能让您的计算机工作 的一系列基本程序和实用工具; 大多数的软件至少都要卖几百块钱,您们怎么愿意白白把它送给别人? 您真正应该问的问题是软件公司怎么可以要您花那么多钱买他们的软件.写软件和制 ...

  8. Parallels destop8 无法创建bootcamp虚拟机

    创建基于Boot Camp的虚拟机时弹出“PRL_ERR_DISK_FILE_OPEN_ERROR (0x80021014)”错误提示,由于Mac系统权限错误或Boot Camp内Windows系统权 ...

  9. IDA strings view 中文字符的显示

    具体原理不清楚,在网上找了找,记录下. 第一步:将ida.cfg中cpp866 version的AsciiStringChars注释掉,把full version的AsciiStringChars取消 ...

  10. uploadify控件使用在.net

    第一次是博客,还有丢丢小兴奋呢.作为一个资深菜鸟,为了给自己留下点什么,开始记录一些技术问题.当然也是学习过程.    下面是成品的在.net web下的应用,还有很多不足的地方,期待大家的点评. $ ...