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 = {A
1, A
2,..., A
N}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B
1, B
2,... , B
N} 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 A i.
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 A i.
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
Recommend
题意:
给定一个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;
}