luogu P3936 Coloring
题目描述
在一个 n×mn\times mn×m 的矩阵中,每个点都染了一种颜色(只能是 [1,c][1,c][1,c] 中的一种),求一种方案,使得相邻异色点对数最小。
Solution 3936\text{Solution 3936}Solution 3936
这是一道好题。正所谓代码五分钟,调参两百年。
随机一个矩阵作为初始矩阵。每次降温时尝试交换两个随机元素,判断交换后是否更优。详见代码。
对于每次交换,更新 calccalccalc 函数可以在 O(1)O(1)O(1) 中完成(提示:一个元素仅能与其上下左右相邻的元素产生贡献,仅更新交换的两个元素即可)。这部分请读者自行完成推导。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#define reg register
int n,m,c;
int p[60];
struct node{
int a[30][30];
}o,ans;
int answ=0x3f3f3f3f;
int calc(node a){
int cnt=0;
for(reg int i=1;i<=n;++i)
for(reg int j=1;j<=m;++j){
if(i>=2&&a.a[i][j]!=a.a[i-1][j]) ++cnt;
if(i<=n-1&&a.a[i][j]!=a.a[i+1][j]) ++cnt;
if(j>=2&&a.a[i][j]!=a.a[i][j-1]) ++cnt;
if(j<=n-1&&a.a[i][j]!=a.a[i][j+1]) ++cnt;
}
return cnt/2;
}
void SA(){
double t=1.0;
while(t>1e-15){
node no=ans;
int x1,y1,x2,y2,tt;
do{
x1=rand()%n+1;
y1=rand()%m+1;
x2=rand()%n+1;
y2=rand()%m+1;
}while(x1==x2&&y1==y2);
tt=no.a[x1][y1];no.a[x1][y1]=no.a[x2][y2];no.a[x2][y2]=tt;
int nw=calc(no);
int delta=nw-answ;
if(delta<0){
answ=nw;
ans=o=no;
}
else if(exp(-delta/t)*32767>rand()) o=no;
t*=0.9999;
}
}
void work(){
int now=1,pt=1,xx=1,yy=1; //随便搞一个矩阵作为初始矩阵
for(reg int i=1;i<=n*m;++i){
o.a[xx][yy]=now;
if(yy==m) yy=0,++xx;
if(pt>=p[now]){
pt=0;
++now;
}
++yy;++pt;
}
o.a[n][m]=c;ans=o;
for(reg int i=1;i<=10;++i) SA();
}
int main(){
srand(1007);
scanf("%d%d%d",&n,&m,&c);
for(reg int i=1;i<=c;++i)
scanf("%d",&p[i]);
work();
for(reg int i=1;i<=n;++i){
for(reg int j=1;j<=m;++j)
printf("%d ",ans.a[i][j]);
puts("");
}
// printf("%d",calc(ans));
}
luogu P3936 Coloring的更多相关文章
- 题解 洛谷P3936 Coloring
考虑搜索,发现复杂度爆炸 贪心,正确性过低(~~实测爆炸~~) 于是,~~发现~~这题是模拟退火 这里不讲解退火的定义了,初学退火可以去平衡点 退火本身维护一个答案图像,答案的q,当前图 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- 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 ...
随机推荐
- UEdit插件使用
最近刚被分配了以个消息发布的任务,其中用到了富文本编辑器.以前也用过,不过这次我选择的是百度富文本编辑器ueditor1_4_3-utf8-jsp版的. 其实看ueditor功能很强大,不过百度的设计 ...
- zookeeper伪集群
Zookeeper运行需要java环境,需要安装jdk,建议本地下载好需要的安装包然后上传到服务器上面,服务器上面下载速度太慢. 上传 [root@192 ~]# java -version java ...
- Spring boot 自定义banner的在线制作
目前工作不是很忙,利用闲暇的时间,在给自己不断地充电,提升自己的技术实力. 目前在做一个基于Spring Boot2.x+webmagic+quartz的爬虫项目[hotDog]https://git ...
- Spark开发常用参数
Driver spark.driver.cores driver端分配的核数,默认为1,thriftserver是启动thriftserver服务的机器,资源充足的话可以尽量给多. spark.dri ...
- selenium退出语句区别
selenium关闭窗口有两个方法,close与quit,我们稍作研究便知道这两个方法的区别. 1.看源码或API 这是close()的说明: Closes the current window. 关 ...
- Python学习笔记整理总结【网络编程】【线程/进程/协程/IO多路模型/select/poll/epoll/selector】
一.socket(单链接) 1.socket:应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socke ...
- SpringBoot起飞系列-Web开发(四)
一.前言 从今天你开始我们就开始进行我们的web开发,之前的一篇用SpringBoot起飞系列-使用idea搭建环境(二)已经说明了我们如何进行开发,当然这是搭建起步,接下来我们就开始进行详细的开发, ...
- Spring MVC-从零开始-未完待续
Spring MVC 之 json格式的输入和输出 Spring定时器简单使用 Spring mvc 拦截器的简单使用 Spring MVC文件上传
- invalid comparison: java.util.ArrayList and java.lang.String解决
报错: Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause ...
- 阿里云ESC服务器centos6.9使用及注意事项
阿里云ESC服务器,配置低,但是足够新手尝试操作练习. 使用之前,注意事项: 选择操作系统 设置实例快照 安装终端工具 一,选择操作系统. 可以在购买服务器的时候进行选择系统盘,也可以在购买之后在实例 ...