hdu 4923 Room and Moor
来源:互联网 发布:西安软通高级java工资 编辑:程序博客网 时间:2024/05/17 08:47
Room and Moor
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 675 Accepted Submission(s): 197
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
Author
BUPT
Source
2014 Multi-University Training Contest 6
最后一定是x1,x1...x2,x2....,xm,xm;
那就可以将相同的合并,每一段维护长度与和,这样就可以算出平均值,然后O(N)就可以算出答案了。
代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <functional>#include <sstream>#include <iomanip>#include <cmath>#include <cstdlib>#include <ctime>#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF 1e9#define MAXN 21const int maxn = 100005;#define mod 1000000007#define eps 1e-7#define pi 3.1415926535897932384626433#define rep(i,n) for(int i=0;i<n;i++)#define rep1(i,n) for(int i=1;i<=n;i++)#define scan(n) scanf("%d",&n)#define scanll(n) scanf("%I64d",&n)#define scan2(n,m) scanf("%d%d",&n,&m)#define scans(s) scanf("%s",s);#define ini(a) memset(a,0,sizeof(a))#define out(n) printf("%d\n",n)//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int num[maxn],l[maxn];int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);#endif int n;int T;cin>>T;while(T--){scanf("%d",&n);int cnt = 0;int x;rep(i,n){scan(x);num[cnt] = x;l[cnt++] = 1;while(cnt >= 2){if(num[cnt-1]*1.0/l[cnt-1] <= num[cnt-2]*1.0/l[cnt-2]) //if top's ave xiaoyu ci top's,than merge them{num[cnt-2] += num[cnt-1];l[cnt-2] += l[cnt-1];cnt--;}elsebreak;}}double ans = 0;for(int i = 0;i < cnt; i++){double ave = num[i] * 1.0 / l[i];int num1 = num[i],num0 = l[i] - num[i];;ans += num1 * (1 - ave) * (1 - ave) + num0 * ave * ave;}printf("%.6lf\n",ans);}return 0;}
0 0
- 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(瞎搞题)
- hdu 4923 Room and Moor 贪心+YY
- HDU 4923 Room and Moor【栈】【想法】
- HDU 4923 - Room and Moor (贪心)
- HDU 4923 Room and Moor 贪心+栈
- Thinkphp中使用smarty模板引擎时在U函数中使用变量的问题
- 乔布斯未完成的梦想
- JavaScript学习久 jquery4 向xx中增加元素
- 车牌识别
- UIPivkerView 视图中,左边一列变化,右边一列也相应方式变化
- hdu 4923 Room and Moor
- JAVA的系统时间输出以及判断今天是星期几
- 工程项目责任成本承包工作的探索之一
- Linux Mint 17 Cinnamon技巧集锦
- jetty-maven-plugin 热加载静态文件
- Mysql----浅入浅出之插入删除等操作
- hdu 2102 A计划 (bfs+队列)
- Decorator装饰模式
- 链接分析算法系列-机器学习排序