2680. 均分数据

题目链接

2680. 均分数据

已知 N 个正整数:A1A2An

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

均方差公式如下:

σ=i=1n(xix¯)2n,x¯=i=1nxin

其中 σ 为均方差,x¯ 为各组数据和的平均值,xi 为第 i 组数据的数值和。

输入格式

第一行是两个整数,表示 N,M 的值(N 是整数个数,M 是要分成的组数)。

第二行有 N 个整数,表示 A1A2An。(同一行的整数间用空格分开)

输出格式

包括一行,这一行只包含一个数,表示最小均方差的值(保留小数点后两位数字)。

数据范围

MN20,
2M6,
1Ai50

输入样例:

6 3 1 2 3 4 5 6

输出样例:

0.00

解题思路

模拟退火

将每个数放入某一组中有一个贪心策略:每次将该数放和最小的组中

这样的策略不一定能构造出答案,但是如果每次模拟退火将序列随机化就一定可以构造出答案,另外构造出的序列交换两点变化不大,即函数具有一定的连续性,故可用模拟退火求解

  • 时间复杂度:O()

代码

// Problem: 均分数据 // Contest: AcWing // URL: https://www.acwing.com/problem/content/2682/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=25,M=10; int n,m,w[N],s[N]; double res=1e9; double cal() { memset(s,0,sizeof s); for(int i=0;i<n;i++) { int k=0; for(int j=0;j<m;j++) if(s[j]<s[k])k=j; s[k]+=w[i]; } double avg=0,ret=0; for(int i=0;i<m;i++)avg+=(double)s[i]/m; for(int i=0;i<m;i++)ret+=(s[i]-avg)*(s[i]-avg); res=min(res,sqrt(ret/m)); return ret; } void aimulate_anneal() { random_shuffle(w,w+n); for(double t=1e4;t>1e-4;t*=0.99) { int a=rand()%n,b=rand()%n; double x=cal(); swap(w[a],w[b]); double y=cal(); double delta=y-x; if(exp(-delta/t)<(double)rand()/RAND_MAX)swap(w[a],w[b]); } } int main() { cin>>n>>m; for(int i=0;i<n;i++)cin>>w[i]; while((double)clock()/CLOCKS_PER_SEC<0.8)aimulate_anneal(); printf("%.2lf",res); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16748319.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(145)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-10-02 牛客2018暑假多校训练营2
点击右上角即可分享
微信分享提示