一道状压题,但今天闲来无事又用遗传乱搞了一下。

  设了一个DNA数组,DNA[i]记录第i个物品放在哪个组里。适应度是n-这个生物的组数+1.

  交配选用的是轮盘赌和单亲繁殖——0.3的几率单点变异。(事实上有性生殖我似乎写不出来……代码量略大)

  种群大小开到了400,在vijos上繁殖了2050代,下数据自己测也是对的。

然而只有84分

  这究竟是为什么啊    下数据自己测是没错的啊………………

  疯了

  代码和数据先放到这里,以后再改吧

  

#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<ctime> inline double random(double from,double to){
return rand()*1.0/RAND_MAX*(to-from)+from;
} inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int n;
int w[],W;
int ans=0x7fffffff; struct Creative{
int DNA[],s[];
int cnt,score;
Creative(){ cnt=;score=; memset(s,,sizeof(s)); }
Creative Create(){
Creative New;
for(register int i=;i<=n;++i){
if(New.cnt==||random(,)<=0.5){
New.cnt++;
New.DNA[i]=New.cnt;
New.s[New.cnt]+=w[i];
}
else{
int j,tot=;
Label:
tot++;
j=random(,New.cnt);
if(New.s[j]+w[i]>W&&tot<=n*) goto Label;
if(!New.s[j]&&tot<=n+) goto Label;
if(tot>=n*){
New.cnt++;
New.DNA[i]=New.cnt;
New.s[New.cnt]+=w[i];
continue;
}
New.DNA[i]=j;
New.s[j]+=w[i];
}
}
if(New.cnt) ans=std::min(ans,New.cnt);
New.score=n-New.cnt+;
return New;
}
}; struct Biome{
Creative e[];
int tot;
void Clear(bool flag){
tot=;
for(int i=;i<;++i){
if(flag) e[i]=e[i].Create();
if(e[i].cnt) tot+=e[i].score;
if(e[i].cnt==) i--;
}
}
int Find(){
int sum=,limit=random(,tot);
for(register int i=;i<;++i){
sum+=e[i].score;
if(sum>=limit) return i;
}
return ;
}
Creative Multi(int x){
Creative New=e[x];
if(random(,)>0.3) return New;
int i=random(,n+),pos=New.DNA[i];
New.s[pos]-=w[i];
if(!New.s[pos]){
New.cnt--;
New.score++;
}
int j,tot=;
Label:
tot++;
j=random(,n);
if(New.s[j]+w[i]>W&&tot<=n*) goto Label;
if(!New.s[j]&&tot<=n+) goto Label;
if(tot>=n*){
New.cnt++;
New.DNA[i]=New.cnt;
New.score--;
New.s[New.cnt]+=w[i];
}
else{
New.DNA[i]=j;
if(!New.s[j]){
New.cnt++;
New.score--;
}
New.s[j]+=w[i];
ans=std::min(ans,New.cnt);
}
return New;
}
}Old,New; inline bool cmp(Creative a,Creative b){ return a.score>b.score; } int main(){
srand(time(NULL));
n=read(),W=read();
for(int i=;i<=n;++i) w[i]=read();
Old.Clear();
for(register int T=;T<=;++T){
Old.Clear();
for(register int i=;i<;++i) New.e[i]=Old.Multi(Old.Find());
std::sort(Old.e+,Old.e+,cmp);
std::sort(New.e+,New.e+,cmp);
for(register int i=;i<=;++i) Old.e[+i]=New.e[i];
}
/*for(int i=1;i<=100;++i,printf("\n")){
for(int j=1;j<=n;++j) printf("%d ",Old.e[i].DNA[j]);
printf(">>>%d %d",Old.e[i].cnt,Old.e[i].score);
}*/
printf("%d",ans);
return ;
}

18 100000000
37597832
24520955
100000000
18509980
29141223
20287969
14028193
33097076
12116817
53439913
10216168
32891936
43952038
13463011
4056577
4646046
10153053
37881213

下一个准备用遗传搞的题是猫狗大战。

【Luogu】P3052摩天大楼里的奶牛(遗传算法乱搞)的更多相关文章

  1. 【Luogu】P3052摩天大楼里的奶牛(状压DP)

    参见ZHT467的题解. f[i]表示在i这个集合下的最少分组数和当前组最少的容量. 从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算. 代码如下 #includ ...

  2. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]

    题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...

  3. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  4. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  5. 【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper

    迭代加深搜索基础 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A ...

  6. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  7. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...

  8. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  9. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

随机推荐

  1. tomcat配置 —— 各个目录的作用

    tomcat各目录(文件)作用 tomcat-7.0.50解压版,主目录一览: 我们可以看到主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹,下面对他们分别 ...

  2. Fedora19 有关输入法的无法切换问题 和 终端的快捷设置问题

    Fedora19 有关输入法的无法切换问题 和 终端的快捷设置问题 1.首先,要单击右上角的设置输入法的"区域与语言设置",要设置为“为每个窗口设置不同的输入源”. 还有,刚使用的 ...

  3. MVC简单登陆验证

    配置文件: <system.web> <authentication mode="Forms"> <!-- 如果验证失败就返回URL的指定界面,设置c ...

  4. python之道04

    1.写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...

  5. Spring-2-官网学习

    spring生命周期回调 结合生命周期机制(官网提供) 1.实现InitializingBean接口重写void afterPropertiesSet() throws Exception;方法 使用 ...

  6. vuejs 中 select 动态填充数据,后台的数据

           selected:"A" 对       selected:A 错.  变量不用引号. 内容一定要引号. https://jsfiddle.net/rgnuaw30/ ...

  7. shell脚本,awk如何处理文件中上下关联的两行。

    文件d.txt如下内容 ggg 1portals: 192.168.5.41:3260werew 2portals: 192.168.5.43:3260 如何把文件d.txt内容变为如下内容 ggg  ...

  8. 268. Missing Number@python

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. Ubuntu 18.04修改默认源

    安装Ubuntu 18.04后,使用国外源太慢了,修改为国内源会快很多. 修改阿里源为Ubuntu 18.04默认的源 备份/etc/apt/sources.list #备份 cp /etc/apt/ ...

  10. [图文][提供可行性脚本] CentOS 7 Fencing+Pacemaker三节点搭建高可用集群

    实验说明: 实验环境: 宿主机系统   :Fedora 28 WorkStation 虚拟机管理器 :Virt-Manager 1.5.1 虚拟机配置   :ha1  CentOS 7.2 1511 ...