hdu4923(想法题)
来源:互联网 发布:微商城软件下载 编辑:程序博客网 时间:2024/06/05 07:48
题意:给一个有0 和 1 组成的序列A,求一个B序列,满足三个条件,的最优值
这是一道想法题。
预处理:
1、 将最前面的0去掉,将最后面的1去掉;
2、 将串分成若干个连续的1+连续的0,比如1100111000,分成1100, 111000 ,记为a1b1,a2b2.....
3、 x1 = a1/(a1+b1),x2 = a2/(a2+b2)......如果rate是递增的,那么就简单了,但是这里并不一定是递增的;
这是一道想法题。
预处理:
1、 将最前面的0去掉,将最后面的1去掉;
2、 将串分成若干个连续的1+连续的0,比如1100111000,分成1100, 111000 ,记为a1b1,a2b2.....
3、 x1 = a1/(a1+b1),x2 = a2/(a2+b2)......如果rate是递增的,那么就简单了,但是这里并不一定是递增的;
4、
如果看懂了上图的话,那么这题就可以解决了,这里还需要一个栈来模拟这个过程
代码如下:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<math.h>#define N 100005#define inf 0x7ffffff#define eps 1e-9#define pi acos(-1.0)using namespace std;int a[N];struct node{ int num1,num0; double rate;}b[N];stack<node> q;int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout); int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int i; for(i = 0; i < n; i++) scanf("%d",&a[i]); i = 0; while(a[i] == 0) i++; while(a[n-1] == 1) n--; a[n] = -1; int temp = 0,flag = 0,tot = 0; for(; i < n; i++) { temp++; if(a[i] != a[i+1]){ if(flag%2 == 0) b[tot].num1 = temp; else b[tot++].num0 = temp; temp = 0; flag++; } } while(!q.empty()) q.pop(); for(i = 0; i < tot; i++) { double rat = 1.0*b[i].num1/(b[i].num1+b[i].num0); node tmp; tmp.rate = rat; tmp.num1 = b[i].num1; tmp.num0 = b[i].num0; while(1) { if(q.size() == 0 || q.top().rate < tmp.rate) { q.push(tmp); break; } node tmp1 = q.top(); q.pop(); tmp.num1 += tmp1.num1; tmp.num0 += tmp1.num0; tmp.rate = 1.0*tmp.num1/(tmp.num1+tmp.num0); } } double ans = 0; while(!q.empty()) { node tmp = q.top(); q.pop(); ans = ans + 1.0*tmp.num1*(1.0 - tmp.rate)*(1.0 - tmp.rate) + 1.0*tmp.num0*tmp.rate*tmp.rate; } printf("%.6lf\n",ans); } return 0;}
0 0
- hdu4923(想法题)
- hdu4923(2014多校第六场第三题)
- hdu6105(想法题)
- hdu6105(想法题)
- HDU4923 Room and Moor
- hdu4923 Room and Moor
- hdu 4811(想法题)
- hdu 5744(想法题)
- hdu 4803 (想法题 卡精度)
- UVA 1450 Airport(二分+想法题)
- hdu 5358 First One(想法题)
- hdu5386Cover六校(想法题)
- Largest Point(想法题+贪心思想)
- UVALive5052 Genome Evolution(想法题)
- Codeforces 733D 立方体(想法题)
- hdu4923 Room and Moor 单调栈
- hdu4923 f(A,B)分段处理
- ACM想法题
- iOS中通过NSDate获取星期几、星期的方法
- WINDOWS PHONE8 开发 (教你获得WP8开发者账号)
- Unity 3D引擎:十大最火的插件推荐
- Android SAX解析xml文件
- Android设置应用界面显示方向
- hdu4923(想法题)
- freemaker 常用语法总结
- Tair LDB基于Prefixkey的范围查找
- java内存解析
- SecureCRT中使用上传下载文件工具rz和sz
- 风水如何提升领导运 沙发最宜紧靠客厅主位
- Android手机短信与通讯录存储位置
- STL vector中的rbegin方法(5)
- memcached refcount