题目传送门

这道题可以用C++的random_shuffle屮过去。

random数列插入顺序,每次都插入数值和最小的那一组。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int N,M,a[];
double ave,ans=2e9,t[]; double get()
{
double tot=;
memset(t,,sizeof t);
for(int i=;i<=N;i++){
int d,mx=2e9;
for(int j=;j<=M;j++)if(t[j]<mx)mx=t[j],d=j;
t[d]+=a[i];
}
for(int i=;i<=M;i++)
tot+=(ave-t[i])*(ave-t[i]);
return tot;
} int main()
{
srand();
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)scanf("%d",&a[i]),ave+=a[i];ave/=M;
for(int i=;i<=;i++){
random_shuffle(a+,a+N+);
ans=min(ans,get());
}
printf("%.2lf",sqrt(ans/M));
return ;
}

random_shuffle

这道题也可以用模拟退火。

设定一个初始温度,然后降温,降温的同时如果大于某个你设定的温度,即当前状态不稳定,进行贪心。

反之为稳定可以随机一个放到随机一个组里。

code:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; int N,M,w[],S=;
double ans,res=2e9,t[],ave,a[]; void work(int x,int y)
{
t[w[x]]-=a[x];
t[y]+=a[x];
w[x]=y;
} double get()
{
double tot=;
for(int i=;i<=M;i++)
tot+=(ave-t[i])*(ave-t[i]);
return tot;
} int main()
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)
scanf("%lf",&a[i]),ave+=a[i];
ave/=M;
srand();
while(S--){
memset(t,,sizeof t);
memset(w,,sizeof w);
for(int i=;i<=N;i++)
t[w[i]=rand()%M+]+=a[i];
ans=get();
for(double Tempr=;Tempr>;Tempr*=0.812){
int x=rand()%N+,y=,bef;
if(Tempr<)y=rand()%M+;
else for(int i=;i<=M;i++)if(t[y]>t[i])y=i;
bef=w[x];
work(x,y);
double now=get();
if(now<ans||Tempr>rand()%)ans=now;
else work(x,bef);
}
res=min(res,ans);
}
printf("%.2lf",sqrt(res/M));
return ;
}

最新文章

  1. ASP.NET图形验证码的生成
  2. JAVA NIO——Buffer和FileChannel
  3. 23. javacript高级程序设计-数据存储
  4. Android基础之项目结构分析
  5. ZENCART 打开/关闭日志文件
  6. YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE
  7. Oracle EBS-SQL (MRP-5):重起MRP Manager.sql
  8. Android 高仿微信即时聊天 百度云为基础的推
  9. ajax遇到的问题
  10. 从源码理解Spring原理,并用代码实现简易Spring框架
  11. [js高手之路]深入浅出webpack教程系列3-配置文件webpack.config.js详解(下)
  12. Oracle通过JOB定时执行存储过程实现两表数据比对
  13. Xamarin控件使用之ListView
  14. 我的redis入门之路
  15. Hdoj 1203.I NEED A OFFER! 题解
  16. IIS 使用域账户访问SQL 需要配置
  17. The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized or represents more than one time zone问题解决
  18. 安装CentOS 6.x出现Disk sda contains BIOS RAID metadata
  19. Linux下MariaDB 安装及root密码设置(修改)
  20. 【二分+SPFA】修建道路(road)

热门文章

  1. STL中set和map
  2. Coroutine及其实现
  3. C#的Lambda 表达式都使用 Lambda 运算符 =&gt;,该运算符读为“goes to”。语法如下:
  4. 转:iBatis简单入门教程
  5. 初始Flask
  6. Elasticsearch安装记录
  7. 用UIScrollView产生视差效果
  8. 使用SDWebImage淡入淡出的方式加载图片
  9. HTTP 头 Connection:close 作用
  10. PHP CLI模式下echo换行