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 ...
随机推荐
- CentOS 7 中 Docker 的安装
CentOS 7 中 Docker 的安装 Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想要安装 docker,只需要运行下面的 yum 命令: [root@loc ...
- Maven整合Spring与Solr
首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖: <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- [ python ] 接口类和抽象类
接口类 继承有两种用途:1. 继承基类的方法,并且做出自己的改变或者扩展(代码重用)2. 申明某个子类兼容于某基类,定义一个接口类interface,接口类定义了一些接口名且未实现接口的功能,子类继承 ...
- C#子线程中更新主线程UI-----注意项
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- logstash参数配置
input配置: file:读取文件 input { file{ path => ["/var/log/*.log","/var/log/message" ...
- ubuntu 依赖问题
ubuntu想装个QQ,无奈安装不但出错,还导致现在的软件依赖出了问题 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 您也许需要运行“apt --fix-b ...
- ubuntu 系统提示升级失败,boot空间不足
系统提示升级失败,boot空间不足,解决方法: linux 随着系统的升级,会自动攒下好几个内核 执行 uname -a 看下自己当前启动的是哪个内核 dpkg --get-selections |g ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- ubuntu1604 安装samba
ubuntu1604 安装samba sudo apt-get install samba 更改/etc/samba/smb.conf添加[share]comment = sharepath = /h ...
- 【WPF】奇怪的INotifyPropertyChanged的实现
MSDN是这样解释的: INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Per ...