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.
 

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
原创粉丝点击