[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 ... 
随机推荐
- Qt滑动条设计与实现
			没有找到Qt的滑动条控件,所以自己写了一个,能够实现亮度调节.音量调节等功能. 效果如下图: 主要设计思路: 有些调节功能如对比度是有负值的,所以需要能对滑动条的数值范围进行设置,不局限于0~100 ... 
- 【Tools】Pycharm 2018专业版 linux安装教程 附2018专业版密钥
			Linux安装pycharm2018专业版 1. 下载安装包 Pycharm下载地址:http://www.jetbrains.com/pycharm/download/ 2.终端打开你的安装包所在路 ... 
- CocosCreator游戏开发---菜鸟学习之路(二)SocketIO简易教程
			请先参考教程司令部-SocketIO教程进行相关操作 开发完成后部分用户会出现持续输出 a userConnected的BUG 如下图所示 经过一段时间的BUG检查终于发现了问题所在.每个人碰到的情况 ... 
- [bzoj4551][Tjoi2016&Heoi2016]树-树链剖分
			Brief Description 给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个 结点,可以打多次标记.) ... 
- c中const定义的问题
			/* 这题有个疑问: const double BASE1=BREAK1*RATE1; //第二个分界点前总共要缴的税收 const double BASE2=BASE1+(BREAK2-BREAK1 ... 
- 阿里巴巴Java开发规约插件地址
			Git地址: https://github.com/alibaba/p3c eclipse 安装地址: https://p3c.alibaba.com/plugin/eclipse/update 
- 拥抱.NET Core系列:MemoryCache 初识
			Cache是一个绝大多数项目会用到的一个技术,说起到缓存可能就联想到 Set.Add.Get.Remove.Clear 这几个方法.那么在.NET Core中微软给我们带来了什么样的缓存体验呢?今天我 ... 
- php常见面试题
			1.如何取得来访者的IP地址? $_SERVER['RRMOTE_ADDR']; $_SERVER['CLIENT_IP']; $_SERVER['HTTP_X_FORWARED_FOR']; 2.$ ... 
- 《HelloGitHub》第 23 期
			公告 新的一年,不忘初心,从新开始.加油! <HelloGitHub>第 23 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的 ... 
- Web渗透测试笔记(基础部分)
			信息收集: dns信息收集 格式:dns... -参数 域名 -参数 示例: root@xxSec:~# dnsenum baidu.com root@xxSec:~# dnsenum -f dns. ... 
