[Luogu3936]Coloring
sol
模拟退火呀
初始状态按顺序涂色,让同种颜色尽量放在一起。
每次随机交换两个位置,注意\(\Delta\)的计算
瞎JB调一下参数就行了
可以多做几次避免陷入局部最优解
code
#include<cstdio>
#include<algorithm>
#include<ctime>
#include<cmath>
using namespace std;
const int N = 100;
struct node{int a[25][25],tot;}ans;
int n,m,c,p[N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
double Rand(){return rand()%1000/1000.0;}
bool inside(int x,int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
void SA(double T)
{
node now=ans,nw;
int x1,y1,x2,y2,d;
while (T>1e-15)
{
nw=now;
x1=1+rand()%n,y1=1+rand()%m;
x2=1+rand()%n,y2=1+rand()%m;
if (x1==x2&&y1==y2) continue;
for (int d=0;d<4;d++)
if (inside(x1+dx[d],y1+dy[d]))
nw.tot-=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
for (int d=0;d<4;d++)
if (inside(x2+dx[d],y2+dy[d]))
nw.tot-=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
swap(nw.a[x1][y1],nw.a[x2][y2]);
for (int d=0;d<4;d++)
if (inside(x1+dx[d],y1+dy[d]))
nw.tot+=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
for (int d=0;d<4;d++)
if (inside(x2+dx[d],y2+dy[d]))
nw.tot+=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
if (nw.tot<=now.tot||exp((now.tot-nw.tot)/T)>Rand()) now=nw;
if (nw.tot<ans.tot) ans=nw;
T*=0.99999;
}
}
int main()
{
scanf("%d %d %d",&n,&m,&c);
for (int i=1;i<=c;++i) scanf("%d",&p[i]);
for (int i=1,k=1;i<=n;i++)
for (int j=1;j<=m;j++)
ans.a[i][j]=p[k]?k:++k,--p[k];
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
{
if (i<n&&ans.a[i][j]!=ans.a[i+1][j]) ++ans.tot;
if (j<m&&ans.a[i][j]!=ans.a[i][j+1]) ++ans.tot;
}
SA(1);SA(1);SA(1);
for (int i=1;i<=n;++i,puts(""))
for (int j=1;j<=m;++j)
printf("%d ",ans.a[i][j]);
return 0;
}
[Luogu3936]Coloring的更多相关文章
- Luogu3936 Coloring(模拟退火)
裸退火,每次交换两个格子即可.依旧不会调参,稍微抄了点参数并且把随机种子设成了一个神奇的数字终于过掉了. #include<iostream> #include<cstdio> ...
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees DP
C. Coloring Trees ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...
- CodeForces #369 C. Coloring Trees DP
题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少. K:连续的颜色为一组 ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- C. Coloring Trees DP
传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...
- codeforces 711C C. Coloring Trees(dp)
题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
随机推荐
- elk安装配置
ELK介绍 官网https://www.elastic.co/cn/ 中文指南https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/det ...
- 用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS
HTTP 协议是不加密传输数据的,也就是用户跟你的网站之间传递数据有可能在途中被截获,破解传递的真实内容,所以使用不加密的 HTTP 的网站是不太安全的.所以, Google 的 Chrome 浏览器 ...
- 如何知道你的linux是什么时候安装的
在安装系统时,每个分区下都会有一个 lost+found,而且这个目录的创建时间是和该分区创建的时间一样的.所以如果想知道你的系统是什么时候安装的,只需要看这个目录的创建时间即可. 通常情况下,我们分 ...
- Filezilla Server日志文件
Filezilla Server版本:0.9.41. Filezilla Server日志文件在软件安装目录下Logs目录下. 但Filezilla Server默认不开始日志记录,如何开启filez ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- 腾讯云微计算实践:从Serverless说起,谈谈边缘计算的未来
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:黄文俊,腾讯云高级产品经理,曾经历过企业级存储.企业级容器平台等产品的架构与开发,对容器.微服务.无服务器.DevOps等都有浓厚兴趣. ...
- 用Eclipse Maven 创建 Web 3.0 项目问题 正确的处理步骤
在Eclipse 安装好Maven插件后,创建Maven webapp项目,在工程 properties -> project facets 界面中将 Dynamic Web Module 修改 ...
- 用一个简单的例子比较SVM,MARS以及BRUTO(R语言)
背景重述 本文是ESL: 12.3 支持向量机和核中表12.2的重现过程.具体问题如下: 在两个类别中产生100个观测值.第一类有4个标准正态独立特征\(X_1,X_2,X_3,X_4\).第二类也有 ...
- 1.2 decimal模块
>>> 0.1 + 0.1 +0.1 == 0.3 False >>> >>> print(0.1 + 0.1 + 0.1) 0.30000000 ...
- hdu1496 打表
通常可以想到直接四个for枚举,但是会超时.就算只用三个for也很危险.可以用打表的方法将时间复杂度降到O(n^2),注意到x1,x2,x3,x4的取值区间是关于零对称的,因此可以只考虑正整数部分,洗 ...