Luogu

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的更多相关文章

  1. Luogu3936 Coloring(模拟退火)

    裸退火,每次交换两个格子即可.依旧不会调参,稍微抄了点参数并且把随机种子设成了一个神奇的数字终于过掉了. #include<iostream> #include<cstdio> ...

  2. 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 ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. 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 ...

  5. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  6. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  7. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  8. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

随机推荐

  1. [Python Study Notes]电池信息

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  2. 浅学vue

    因之前项目接触了vue,从此我被迷住,简洁而不失优雅,小巧而不乏大匠. 首先我们要了解vue,什么是vue,正如官网所说:Vue.js 是一套构建用户界面的渐进式框架,Vue 的核心库只关注视图层.V ...

  3. 异步任务利器Celery(一)介绍

    django项目开发中遇到过一些问题,发送请求后服务器要进行一系列耗时非常长的操作,用户要等待很久的时间.可不可以立刻对用户返回响应,然后在后台运行那些操作呢? crontab定时任务很难达到这样的要 ...

  4. Apache设置二级域名和虚拟主机

    apache  httpd.conf 最后: ------------------------------NameVirtualHost *:80<VirtualHost *:80>    ...

  5. H5 拖拽,一个函数搞定,直接指定对象设置可拖拽

    页面上,弹个小窗体,想让它可以拖拽,又不想 加载一堆js,就简单的能让他可以拖动? 嗯,下面有这样一个函数,调用下就好了! 1. 先来说说 H5的 拖拽 在 HTML5 中,拖放是标准的一部分,任何元 ...

  6. Sql Server 常用事务处理总结

    在数据库操作中,常用事务写法: 1. 通过 @@error 判断一批sql 执行完毕,是否有异常.  @@error 为系统变量,每次执行完 sql 都会返回一个数值,  0 表示 执行成功 ,非0 ...

  7. spring cron 定时任务

    文章首发于个人博客:https://yeyouluo.github.io 0 预备知识:cron表达式 见 <5 参考>一节. 1 环境 eclipse mars2 + Maven3.3. ...

  8. java并发 - 自底向上的原理分析

    [TOC] 事先声明,我只是java并发的新手,这篇文章也只是我阅读<java并发编程的艺术>一书(内容主要涉及前3章)的一些总结和感悟.希望大家能多多讨论,对于错误的地方还请指出. 0. ...

  9. Android开发学习必备的java知识

    Android开发学习必备的java知识本讲内容:对象.标识符.关键字.变量.常量.字面值.基本数据类型.整数.浮点数.布尔型.字符型.赋值.注释 Java作为一门语言,必然有他的语法规则.学习编程语 ...

  10. jmeter压力测试笔记- HTTP协议

    一.目标 使用jmeter进行HTTP接口压力测试: 命令行方式运行,方便在linux环境运行: 二.面临的问题 支持多环境测试(开发.测试.生产环境) 支持用户数据.线程数.循环次数等配置在运行时自 ...