hdu 4923 Room and Moor 单调栈
来源:互联网 发布:怎么成为阿里云代理商 编辑:程序博客网 时间:2024/05/21 05:21
首先可以忽略前面的0,和后面的1.剩下的就是由 a个1 b个0 组成的01串。由不等式可知y=(a+b)*x*x-a*x+a;x=a/(a+b)时函数值最小。
而值要求递增,所以把比当前值的段都加入当前段。最后模拟输出即可。
#include <cstring>#include <algorithm>#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <stack>using namespace std;#define maxn 1100000int save[maxn],n;struct node{ int len1,len0;};int main(){ int cas; scanf("%d",&cas); while(cas--) { vector<node>a; stack<node>sta; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&save[i]); } save[n+1]=1; int st=1,ed=n; while(save[st]==0) { st++; } while(save[ed]==1) { ed--; } // printf("%d %d\n",st,ed); if(st==ed+1) { printf("0.000000\n"); continue; } node tmp; tmp.len1=tmp.len0=0; for(int i=st;i<=ed;i++) { if(save[i]==0) { tmp.len0++; if(save[i+1]==1) { a.push_back(tmp); tmp.len0=tmp.len1=0; } } else { tmp.len1++; } } /* for(int i=0;i<a.size();i++) { printf("%d %d\n",a[i].len1,a[i].len0); } */ sta.push(a[0]); node now; for(int i=1;i<a.size();i++) { while(!sta.empty()) { now=sta.top(); if(a[i].len1*(now.len1+now.len0)>now.len1*(a[i].len1+a[i].len0)) { sta.push(a[i]); break; } else { sta.pop(); a[i].len1+=now.len1; a[i].len0+=now.len0; if(sta.empty()) { sta.push(a[i]); break; } } } } double ans=0; while(!sta.empty()) { now=sta.top(); sta.pop(); double x=double(now.len1)/double(now.len1+now.len0); ans+=double(now.len1)*(x-1)*(x-1)+double(now.len0)*x*x; } //ans=ans/double(n); printf("%.6f\n",ans); } return 0;}
0 0
- hdu 4923 Room and Moor 单调栈
- hdu 4923 Room and Moor (单调栈+思维)
- HDU 4923 Room and Moor (数学+单调栈)
- [HDOJ 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 4923 Room and Moor
- hdu 4923 Room and Moor
- Hdu 4923 Room and Moor
- HDU 4923 Room and Moor
- LeetCode_Combination Sum II
- 阿飞对哦ISA家发了我覅哦我
- 男女舞伴贪心
- [转]未知世界
- 关于数组和指针的声明
- hdu 4923 Room and Moor 单调栈
- 终于完成了foggy与jfreechart的初步集成
- Github Mybatis深入学习之Java API .
- WIN7+IIS+PHP配置
- 向量中断和非向量中断
- linux 内核wait queue
- Github Mybatis深入学习之SQL生成类 .
- 8086处理器16位如何20位寻址
- 水樹 奈々 Blog 20140917 川﨑さん、今回もゴッドハンドありがとうございます!(笑) 译文