【Luogu】P3052摩天大楼里的奶牛(遗传算法乱搞)
一道状压题,但今天闲来无事又用遗传乱搞了一下。
设了一个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摩天大楼里的奶牛(遗传算法乱搞)的更多相关文章
- 【Luogu】P3052摩天大楼里的奶牛(状压DP)
参见ZHT467的题解. f[i]表示在i这个集合下的最少分组数和当前组最少的容量. 从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算. 代码如下 #includ ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]
题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper
迭代加深搜索基础 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A ...
- 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 ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...
随机推荐
- iOS 自适应高度,改变字体颜色
#define kMainBoundsWidth ([UIScreen mainScreen].bounds).size.width //屏幕的宽度 #define kFont [UIFont sys ...
- 绘制surfaceView 基础类
public class SurfaceViewTempalte extends SurfaceView implements Callback, Runnable { private Surface ...
- codevs 1487 大批整数排序(水题日常)
时间限制: 3 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description !!!CodeVS开发者有话说: codevs自从换了评测机,新评测机的内存计算 ...
- Google Colab免费GPU使用教程(一)
一.前言 现在你可以开发Deep Learning Applications在Google Colaboratory,它自带免费的Tesla K80 GPU.重点是免费.免费!(国内可能需要tz) 这 ...
- Mysql闪回工具之binlog2sql的原理及其使用
生产上误删数据.误改数据的现象也是时常发生的现象,作为运维这时候就需要出来补锅了,最开始的做法是恢复备份,然后从中找到需要的数据再进行修复,但是这个时间太长了,对于大表少数数据的修复来讲,动作太大,成 ...
- python基础一 day11 装饰器(1)
接收的时候是聚合,调用的时候是打散 print(*args)本来在里面用的时候是用args,是一个元祖,加上一个 * 号,把元祖解包了(打散了). from functools import ...
- python_111_动态导入模块
lib下aa.py文件内容: class C: def __init__(self): self.name='alex' from lib import aa#正常导入 print(aa.C) 动态导 ...
- #include <> 和 #inlude ""的区别
#include < >引用的是编译器的类库路径里面的头文件#include " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...
- tomcat假死现象 - 二
1 编写背景 最近服务器发现tomcat的应用会偶尔出现无法访问的情况.经过一段时间的观察最近又发现有台tomcat的应用出现了无法访问情况.简单描述下该台tomcat当时具体的表现:客户端请求没有响 ...
- javase(10)_多线程基础
一.排队等待 1.下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件 ...