题目大意:

已知N个正整数

将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小

求最小均方差

思路:

模拟退火

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ll long long
#define MAXN 10100
#define inf 2139062143
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
double ans=inf,avg;
int n,m,a[MAXN],s[MAXN],bl[MAXN];
void work()
{
double T,tmp,res=;
memset(s,,sizeof(s));
for(int i=;i<=n;i++)
bl[i]=rand()%m+,s[bl[i]]+=a[i];
for(int i=;i<=m;i++) res+=(s[i]-avg)*(s[i]-avg);
T=;
while(T>0.1)
{
T*=0.9;
int t=rand()%n+,x=bl[t],y;
if(T>) y=min_element(s+,s+m+)-s;
else y=rand()%m+;
if(x==y) continue;
tmp=res;
res-=(s[x]-avg)*(s[x]-avg)+(s[y]-avg)*(s[y]-avg);
s[x]-=a[t],s[y]+=a[t];
res+=(s[x]-avg)*(s[x]-avg)+(s[y]-avg)*(s[y]-avg);
if(res<=tmp) bl[t]=y;
else if(rand()%>T) s[x]+=a[t],s[y]-=a[t],res=tmp;
else bl[t]=y;
}
if(res<ans) ans=res;
}
int main()
{
srand();
n=read(),m=read();
for(int i=;i<=n;i++) a[i]=read(),avg+=a[i];
avg/=(double)m;
for(int i=;i<=;i++) work();
printf("%.2lf\n",sqrt(ans/m));
}

bzoj 2428 均分数据的更多相关文章

  1. bzoj 2428 均分数据 模拟退火

    模拟退火 按照自己的思路打了,结果WA,发现退火最关键的就是初温,降温,和修改次数, 这个题还在外层带了一个循环,骚气 #include<cstdio> #include<iostr ...

  2. bzoj 2428: [HAOI2006]均分数据 随机化

    2428: [HAOI2006]均分数据 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

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

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

  4. bzoj2428: [HAOI2006]均分数据

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

  5. 【BZOJ2428】均分数据(模拟退火)

    [BZOJ2428]均分数据(模拟退火) 题面 BZOJ 题解 先说说黄学长的做法: 当温度比较高的时候,贪心 每次随机一个数,把他放进当前和最少的那一组里面 温度足够低的时候就完全随机然后转移 这样 ...

  6. BZOJ2428 均分数据

    2428: [HAOI2006]均分数据 Time Limit: 5 Sec  Memory Limit: 128 MB Description 已知N个正整数:A1.A2.…….An .今要将它们分 ...

  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. 洛谷 P2503 [HAOI2006]均分数据 随机化贪心

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

随机推荐

  1. CSS 全局样式

    设置全局 CSS 样式:基本的 HTML 元素均可以通过 class 设置样式并得到增强效果:还有先进的栅格系统.

  2. 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西

    知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...

  3. SQL-如何使用 MongoDB和PyMongo。

    先决条件 在开始之前,请确保已经安装了 PyMongo 发行版. 在 Python shell 中,下面的代码应该在不引发异常的情况下运行: >>> import pymongo 假 ...

  4. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  5. <vue>…… v-if 与 v-show ……//

    #v-if 用法: 根据表达式的值的真假条件渲染元素.在切换时元素及它的数据绑定 / 组件被销毁并重建.如果元素是 <template> ,将提出它的内容作为条件块. 当条件变化时该指令触 ...

  6. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. shit layui & select & re-render & bug

    shit layui https://www.layui.com/doc/modules/form.html#onselect https://www.layui.com/doc/element/fo ...

  8. CentOS 7 & Chinese Fonts bug

    CentOS 7 & Chinese Fonts bug # check $ yum grouplist $ yum grouplist hidden # root $ yum groupin ...

  9. Partitioned Replacement for Cache Memory

    In a particular embodiment, a circuit device includes a translation look-aside buffer (TLB) configur ...

  10. 新vim配置文件

    "******************************************************特殊设置************************************ ...