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 ...
随机推荐
- defer用途
package main /* defer :程序退出时执行,先进后执行 defer庸碌: 1.关闭文件句柄 2.锁资源释放 3.数据库连接释放 */ import ( "fmt" ...
- git命令大全【转】
转自:http://www.jqhtml.com/8235.html 初始化本地git仓库(创建新仓库) git init 配置用户名 git config --global user.name &q ...
- Repository.UpdateModel(model, db);
Repository.UpdateModel(model, db); 可用类型 string int? datetime 不可用类型 int double
- selenium grid结构图
调用 Selenium-Grid 的基本结构图如下: 上面是使用 selenium-grid 的一种普通方式,仅仅使用了其支持的分布式执行的功能,即当你同时需 要测试用例比较多时,可以平行的执行这些用 ...
- [How to] 使用HBase协处理器---基本概念和regionObserver的简单实现
1. 简介 对于HBase的协处理器概念可由其官方博文了解:https://blogs.apache.org/hbase/entry/coprocessor_introduction 总体来说其包含两 ...
- Git提交记住用户名和密码
https://www.baidu.com/link?url=R14MHMloypfAfIeiQwCINfY1AZlcoSU7-tYdnqC1PxfmFKs4TWzLOPdtyJbWVfqMqOkRx ...
- LightOJ 1370- Bi-shoe and Phi-shoe (欧拉函数)
题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一个幸运数字, ...
- 谷歌翻译python接口
项目地址: https://github.com/ssut/py-googletrans 安装: sudo pip install googletrans 使用: #!/usr/bin/python ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 小学生都能理解的原生js——call
关于 js 作用域和执行上下文就不过多介绍了,本人也是在网上搜集了各种教程才逐渐理解,以下简单理解并说下call 的作用 首先简单理解下执行上下文有关概念,this 的指向就代表当前执行环境的上下文 ...