HDU 4923 - Room and Moor (贪心)

7 篇文章 0 订阅

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.
 

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
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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值