您的位置:首页 > 其它

HDU 4923 - Room and Moor (贪心)

2014-08-09 20:51 477 查看


Room and Moor


Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)


Total Submission(s): 891 Accepted Submission(s): 273

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

4
9
1 1 1 1 1 0 0 1 1
9
1 1 0 0 1 1 1 1 1
4
0 0 1 1
4
0 1 1 1


Sample Output

1.428571
1.000000
0.000000
0.000000


Author

BUPT

Source

2014 Multi-University Training Contest 6

Recommend

We have carefully selected several similar problems for you: 4930 4929 4928 4927 4926

Statistic | Submit | Discuss | Note

题意:

给定一个n<=100000的01序列A,求另一个不减浮点序列B使得∑(ai-bi)^2最小,输出这个最小值。

看的别人题解

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100000 + 20;

struct Seg {
int sum, len;
double v;
};

int A[maxn];
Seg stac[maxn];
int topc;

int main() {
int T;

scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
topc = 1;
for(int i=1; i<=n; i++) {
scanf("%d", &A[i]);
stac[topc].sum = A[i];
stac[topc].len = 1;
stac[topc].v = (double)A[i] / 1;
topc++;
while(topc > 1 && stac[topc-1].v < stac[topc-2].v) {
stac[topc-2].sum += stac[topc-1].sum;
stac[topc-2].len += stac[topc-1].len;
stac[topc-2].v = (double) stac[topc-2].sum / stac[topc-2].len;
topc--;
}
}
double ans = 0;
for(int i=0; i<topc; i++) {
double x = stac[i].v;
int one = stac[i].sum;
int zero = stac[i].len - one;
ans += one * (1-x) * (1-x) + zero * x * x;
}
printf("%.6lf\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: