(排序+暴力)1098 最小方差

1098 最小方差

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 20 分
  6.  
  7. 3 级题

若x1,x2,x3......xn的平均数为k。

则方差s^2 = 1/n * [(x1-k)^2+(x2-k)^2+.......+(xn-k)^2] 。

方差即偏离平方的均值,称为标准差或均方差,方差描述波动程度。

给出M个数,从中找出N个数,使这N个数方差最小。 收起

输入

第1行:2个数M,N,(M > N, M <= 10000)
第2 - M + 1行:M个数的具体值(0 <= Xi <= 10000)

输出

输出最小方差 * N的整数部分。

输入样例

5 3
1
2
3
4
5

输出样例

2

题解:划重点:方差描述波动程度

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e5+5;
double a[N];
int n,m;
struct fun{
    int x,y;
}f[N];

ll C(int p1,int p2){
    double ave=0;
    for(int i=p1;i<=p2;i++)
        ave+=a[i];
    ave/=n;

    double ans=0;
    for(int i=p1;i<=p2;i++)
        ans+=(a[i]-ave)*(a[i]-ave);
    return (ll)ans;
}
int main(){
    scanf("%d %d",&m,&n);
    for(int i=0;i<m;i++)
        scanf("%lf",&a[i]);
    sort(a,a+m);
    double ans=i_max;
    ll sum=l_max;
    for(int i=0;i<m;i++){
        int j=i+n-1;
        if(j>(m-1)) break;
        sum=min(sum,C(i,j));
    }
    printf("%lld\n",sum);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值