bzoj 2428: [HAOI2006]均分数据 随机化
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
Output
这一行只包含一个数,表示最小均方差的值(保留小数点后两位数字)。
Sample Input
1 2 3 4 5 6
Sample Output
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]均分数据 随机化的更多相关文章
- BZOJ.2428.[HAOI2006]均分数据(随机化贪心/模拟退火)
题目链接 模拟退火: 模拟退火!每次随机一个位置加给sum[]最小的组. 参数真特么玄学啊..气的不想调了(其实就是想刷刷最优解) 如果用DP去算好像更准.. //832kb 428ms #inclu ...
- bzoj 2428: [HAOI2006]均分数据 && bzoj 3680 : 吊打XXX 模拟退火
每次把元素随便扔随机一个初始解,退火时每次随机拿一个元素扔到随机一个集合里,当温度高时因为状态不稳定扔到那个元素和最小的里边. 如果新解优,更新ans. 把原式拆一下,就可以用int存了. bzoj ...
- bzoj 2428: [HAOI2006]均分数据
#include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #inclu ...
- 洛谷 P2503 [HAOI2006]均分数据 随机化贪心
洛谷P2503 [HAOI2006]均分数据(随机化贪心) 现在来看这个题就是水题,但模拟赛时想了1个小时贪心,推了一堆结论,最后发现贪心做 不了, 又想了半个小时dp 发现dp好像也做不了,在随机化 ...
- 2428: [HAOI2006]均分数据
模拟退火.一种十分玄学的随机算法,网上可以查到比较详细的资料. 先随机地把数分成m组,每次随机地选择一个数,一开始直接选最小的一组,后来就随机一组,把这个数换到该组看看答案能不能变小,如果变小则换,如 ...
- bzoj2428: [HAOI2006]均分数据
模拟退火.挺好理解的.然后res打成了ans一直WA一直WA...!!!一定要注意嗷嗷嗷一定要注意嗷嗷嗷一定要注意嗷嗷嗷. 然后我就一直卡一直卡...发现最少1800次的时候就可以出解了.然后我就去调 ...
- P2503 [HAOI2006]均分数据
P2503 [HAOI2006]均分数据 模拟退火+dp (不得不说,我今天欧气爆棚) 随机出1个数列,然后跑一遍dp统计 #include<iostream> #include<c ...
- bzoj2428 [HAOI2006]均分数据 模拟退火
[HAOI2006]均分数据 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 3434 Solved: 1091[Submit][Status][Dis ...
- BZOJ 2428 JZYZOJ1533 : [HAOI2006]均分数据 模拟退火 随机化
http://www.lydsy.com/JudgeOnline/problem.php?id=2428 http://172.20.6.3/Problem_Show.asp?id=1533 http ...
随机推荐
- 【Linux学习】python脚本直接运行与nohup运行结果不同
之前遇到问题,在云主机上运行python脚本直接运行与nohup运行结果不同,甚至nohup根本运行不出来. 后来参考下别人的博客,终于知道问题了. nohup 使用的python版本问题. 而且no ...
- jplayer.js 与 video.js
HTML5 - 两款基于JS的视频播放器 都是基于h5 video 标签,如果不支持则会自动转成flash,这里个人比较推荐使用jplayer; 1.video.js pc端有时候会与视频打交道,如果 ...
- selenium滚动到顶部与底部
#coding=utf-8 from selenium import webdriver #滚动到浏览器顶部 js_top = "var q=document.documentElement ...
- python3.x的HTMLTestRunner.py文件
"""A TestRunner for use with the Python unit testing framework. Itgenerates a HTML re ...
- 画弧线DrawArc的研究-我自己 -- 直线交接圆角
procedure TForm4.Button7Click(Sender: TObject); var pwith: Integer; //画笔的宽度 hx1, hy1: Integer; //横线第 ...
- mac下docker中安装nodejs
一.首先下载docker并安装 https://download.docker.com/mac/stable/Docker.dmg 然后启动docker, 二.获取node最新镜像 输入来着node版 ...
- gentoo emerge unable to sync
gentoo emerge unable to sync Author: Tubo After setting SYNC to customized URL: SYNC="rsync://m ...
- WordPress用户角色与用户能力/权限
WordPress用户角色(user roles)是WP或者其它插件增加的,可以让网站管理员(网站管理员也是一种角色)来方便的管理用户的权限/能力(Capabilities,一般情况下,一种角色不止有 ...
- 全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)
Description 大家都知道最小生成树一般求的是构成最小生成树的边的权值之和. 现在请求构成最小生成树的边的权值之积 S,最终结果请输出 (S % 100003). P.S. 点之间的边为无向边 ...
- python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)
s12-20160116-day03 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...