BZOJ2428_均分数据_KEY
2024-08-24 08:58:26
这道题可以用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 ;
}
最新文章
- ASP.NET图形验证码的生成
- JAVA NIO——Buffer和FileChannel
- 23. javacript高级程序设计-数据存储
- Android基础之项目结构分析
- ZENCART 打开/关闭日志文件
- YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE
- Oracle EBS-SQL (MRP-5):重起MRP Manager.sql
- Android 高仿微信即时聊天 百度云为基础的推
- ajax遇到的问题
- 从源码理解Spring原理,并用代码实现简易Spring框架
- [js高手之路]深入浅出webpack教程系列3-配置文件webpack.config.js详解(下)
- Oracle通过JOB定时执行存储过程实现两表数据比对
- Xamarin控件使用之ListView
- 我的redis入门之路
- Hdoj 1203.I NEED A OFFER! 题解
- IIS 使用域账户访问SQL 需要配置
- 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问题解决
- 安装CentOS 6.x出现Disk sda contains BIOS RAID metadata
- Linux下MariaDB 安装及root密码设置(修改)
- 【二分+SPFA】修建道路(road)