hdu4923 Room and Moor 单调栈
来源:互联网 发布:游戏官方网站源码 编辑:程序博客网 时间:2024/05/21 08:41
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 276 Accepted Submission(s): 79
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.
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
491 1 1 1 1 0 0 1 191 1 0 0 1 1 1 1 140 0 1 140 1 1 1
Sample Output
1.4285711.0000000.0000000.000000
先把前面的1和后面的0去掉。
我当时想的是会不会就取0...x...1这样子,结果不对。。
正确答案是0...x1...x2...x3....1,中间可能有很多取值,满足x1<x2<x3...先把剩下的序列分成一段一段的,连续的1和连续的0为一段,求一个x,为什么连续的1和连续的0为一段呢,因为如果把连续1看成一段,连续0看成一段,1的取值是1,x的取值是0,0<1,所以肯定还是要合并的。为什么这一段只取一个x呢,因为1的段希望x尽量大,0的段希望x尽量小,0段的取值又不能小于1段的取值,所以相等是最优了。x的算法根据抛物线,-b/(2a)。每段求出来的x放到栈里,如果比上段的x小,就要和上段合并,把这段和上段看成一段,再用同样的方法算x,如果又比上段小,继续合并。。
#include<iostream>#include<algorithm>#include<queue>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<set>#include<map>#include<stack>#define INF 0x3f3f3f3f#define MAXN 100010#define MAXM 1500#define MOD 1000000007#define MAXNODE 8*MAXN#define eps 1e-10using namespace std;typedef long long LL;int T,N,a[MAXN],cnt1[MAXN];struct seg{ int s,e; double x;};stack<seg> st;int main(){ freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&a[i]); int p1,p2,flag1=0,flag2=0; for(int i=1;i<=N;i++) if(a[i]!=0){ p1=i; flag1=1; break; } if(!flag1){ printf("0.000000\n"); continue; } for(int i=N;i>=1;i--) if(a[i]!=1){ p2=i; flag2=1; break; } if(!flag2){ printf("0.000000\n"); continue; } if(p1==p2+1){ printf("0.000000\n"); continue; } cnt1[0]=0; for(int i=1;i<=N;i++){ cnt1[i]=cnt1[i-1]; if(a[i]==1) cnt1[i]=cnt1[i-1]+1; } while(!st.empty()) st.pop(); int s,e,i=p1; while(i<=p2){ for(s=i;s<=p2&&a[s]==1;s++); for(e=s;e+1<=p2&&a[e+1]==0;e++); double A=e-i+1,B=-2*(cnt1[e]-cnt1[i-1]),x=(-B)/(2*A); int ls=i; while(!st.empty()&&x<st.top().x){ ls=st.top().s; st.pop(); A=e-ls+1,B=-2*(cnt1[e]-cnt1[ls-1]),x=(-B)/(2*A); } st.push((seg){ls,e,x}); i=e+1; } double ans=0; while(!st.empty()){ seg tmp=st.top(); st.pop(); double A=tmp.e-tmp.s+1,B=-2*(cnt1[tmp.e]-cnt1[tmp.s-1]),C=cnt1[tmp.e]-cnt1[tmp.s-1]; ans+=(4*A*C-B*B)/(4*A); } printf("%.6lf\n",ans); } return 0;}
0 0
- hdu4923 Room and Moor 单调栈
- HDU4923 Room and Moor
- hdu4923 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 (多校第六场C题) 单调栈
- 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
- hdoj 4923 Room and Moor
- hdu 4923 Room and Moor
- HDU 1856(求并查集里元素的个数)
- 树-堆结构练习——合并果子之哈夫曼树
- 利用MFC读取Excel文件
- 计算器(java)
- nformatica学习笔记
- hdu4923 Room and Moor 单调栈
- JAVA容器笔记
- 显示意图激活另外一个activity(检查用户的网络情况2.3.3)
- stars
- linux内核学习(20)设备模型第二层之一
- 打开一个URL并获取http状态码
- OC视频笔记-5
- span与div 行内元素和块元素(标签)
- 关于java中Double类型的运算精度问题