2428: [HAOI2006]均分数据

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=2428

Description

已知N个正整数:A1、A2、……、An 。今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小。均方差公式如下:

,其中σ为均方差,是各组数据和的平均值,xi为第i组数据的数值和。

Input

第一行是两个整数,表示N,M的值(N是整数个数,M是要分成的组数)
第二行有N个整数,表示A1、A2、……、An。整数的范围是1--50。
(同一行的整数间用空格分开)

Output

这一行只包含一个数,表示最小均方差的值(保留小数点后两位数字)。

Sample Input

6 3
1 2 3 4 5 6

Sample Output

0.00

HINT

对于全部的数据,保证有K<=N <= 20,2<=K<=6

题意

题解:

随机化搞搞搞!http://www.cnblogs.com/rausen/p/4324676.html

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const ll inf=1e60;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** double a[maxn];
int p[maxn];
int n,m;
double sum;
double ave;
double ans;
double sqr(double a)
{
return a*a;
}
double solve()
{
int cnt = , sum = , i;
double tmp = , t1, t2;
for (i = ; i <= n && cnt < m; ++i)
if (sum + a[p[i]] >= ave)
{
t1 = sqr(sum - ave), t2 = sqr(sum + a[p[i]] - ave);
if (t1 < t2)
tmp += t1, sum = a[p[i]];
else
tmp += t2, sum = ;
++cnt;
}
else
sum += a[p[i]];
while (i <= n)
sum += a[p[i++]];
tmp += sqr(sum - ave);
return tmp;
}
int main()
{
//freopen("test.txt","r",stdin);
n=read(),m=read();
for(int i=;i<=n;i++)
{
a[i]=read();
sum+=a[i];
p[i]=i;
}
ave=sum/m;
ans=-;
int cntt=;
while(cntt<)
{
random_shuffle(p+,p++n);
if(ans==-)
ans=solve();
else
ans=min(solve(),ans);
cntt++;
}
printf("%.2lf\n",sqrt(ans/m)); }

bzoj 2428: [HAOI2006]均分数据 随机化的更多相关文章

  1. BZOJ.2428.[HAOI2006]均分数据(随机化贪心/模拟退火)

    题目链接 模拟退火: 模拟退火!每次随机一个位置加给sum[]最小的组. 参数真特么玄学啊..气的不想调了(其实就是想刷刷最优解) 如果用DP去算好像更准.. //832kb 428ms #inclu ...

  2. bzoj 2428: [HAOI2006]均分数据 && bzoj 3680 : 吊打XXX 模拟退火

    每次把元素随便扔随机一个初始解,退火时每次随机拿一个元素扔到随机一个集合里,当温度高时因为状态不稳定扔到那个元素和最小的里边. 如果新解优,更新ans. 把原式拆一下,就可以用int存了. bzoj ...

  3. bzoj 2428: [HAOI2006]均分数据

    #include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #inclu ...

  4. 洛谷 P2503 [HAOI2006]均分数据 随机化贪心

    洛谷P2503 [HAOI2006]均分数据(随机化贪心) 现在来看这个题就是水题,但模拟赛时想了1个小时贪心,推了一堆结论,最后发现贪心做 不了, 又想了半个小时dp 发现dp好像也做不了,在随机化 ...

  5. 2428: [HAOI2006]均分数据

    模拟退火.一种十分玄学的随机算法,网上可以查到比较详细的资料. 先随机地把数分成m组,每次随机地选择一个数,一开始直接选最小的一组,后来就随机一组,把这个数换到该组看看答案能不能变小,如果变小则换,如 ...

  6. bzoj2428: [HAOI2006]均分数据

    模拟退火.挺好理解的.然后res打成了ans一直WA一直WA...!!!一定要注意嗷嗷嗷一定要注意嗷嗷嗷一定要注意嗷嗷嗷. 然后我就一直卡一直卡...发现最少1800次的时候就可以出解了.然后我就去调 ...

  7. P2503 [HAOI2006]均分数据

    P2503 [HAOI2006]均分数据 模拟退火+dp (不得不说,我今天欧气爆棚) 随机出1个数列,然后跑一遍dp统计 #include<iostream> #include<c ...

  8. bzoj2428 [HAOI2006]均分数据 模拟退火

    [HAOI2006]均分数据 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 3434  Solved: 1091[Submit][Status][Dis ...

  9. BZOJ 2428 JZYZOJ1533 : [HAOI2006]均分数据 模拟退火 随机化

    http://www.lydsy.com/JudgeOnline/problem.php?id=2428 http://172.20.6.3/Problem_Show.asp?id=1533 http ...

随机推荐

  1. perl6正则 1: ~~ , //, m//, rx//

    ~~ perl6 中, 要匹配一个正则, 使用 ~~ 智能匹配符. > so 'abcde' ~~ /a.c/ True > so 'abcde' ~~ /a.d/ False > ...

  2. Mysql储存过程2:变量定义与参数传递

    #储存过程 中的变量定义 declare 变量名 类型 可选类型 -- 跟建表差不多 create procedure p() begin ); ; select age+number; end$ / ...

  3. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  4. python脚本-实现自动按规则创建指定大小和指定个数的文件案例

    # -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...

  5. Java访问http用户验证

    public class SAXParse { static final String kuser = "admin"; static final String kpass = & ...

  6. leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number

    题目 //像10进制一样进行 转换   只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...

  7. leetcode 之Remove Duplicates from Sorted Array(2)

    描述    Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?    For exampl ...

  8. C语言 五子棋

    #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> ...

  9. IE8/9的几个前端bug解决方案

    最近做的东西需要兼容IE……啊真是令人忧伤…… 解决低版本IE的buggy inline-block 低版本的IE只对本身inline的元素能用inline-block,而本身为block的元素需要设 ...

  10. SGU 206. Roads

    206. Roads time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output: sta ...