Luogu

sol

模拟退火呀

初始状态按顺序涂色,让同种颜色尽量放在一起。

每次随机交换两个位置,注意\(\Delta\)的计算

瞎JB调一下参数就行了

可以多做几次避免陷入局部最优解

code

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<ctime>
  4. #include<cmath>
  5. using namespace std;
  6. const int N = 100;
  7. struct node{int a[25][25],tot;}ans;
  8. int n,m,c,p[N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
  9. double Rand(){return rand()%1000/1000.0;}
  10. bool inside(int x,int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
  11. void SA(double T)
  12. {
  13. node now=ans,nw;
  14. int x1,y1,x2,y2,d;
  15. while (T>1e-15)
  16. {
  17. nw=now;
  18. x1=1+rand()%n,y1=1+rand()%m;
  19. x2=1+rand()%n,y2=1+rand()%m;
  20. if (x1==x2&&y1==y2) continue;
  21. for (int d=0;d<4;d++)
  22. if (inside(x1+dx[d],y1+dy[d]))
  23. nw.tot-=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
  24. for (int d=0;d<4;d++)
  25. if (inside(x2+dx[d],y2+dy[d]))
  26. nw.tot-=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
  27. swap(nw.a[x1][y1],nw.a[x2][y2]);
  28. for (int d=0;d<4;d++)
  29. if (inside(x1+dx[d],y1+dy[d]))
  30. nw.tot+=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
  31. for (int d=0;d<4;d++)
  32. if (inside(x2+dx[d],y2+dy[d]))
  33. nw.tot+=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
  34. if (nw.tot<=now.tot||exp((now.tot-nw.tot)/T)>Rand()) now=nw;
  35. if (nw.tot<ans.tot) ans=nw;
  36. T*=0.99999;
  37. }
  38. }
  39. int main()
  40. {
  41. scanf("%d %d %d",&n,&m,&c);
  42. for (int i=1;i<=c;++i) scanf("%d",&p[i]);
  43. for (int i=1,k=1;i<=n;i++)
  44. for (int j=1;j<=m;j++)
  45. ans.a[i][j]=p[k]?k:++k,--p[k];
  46. for (int i=1;i<=n;++i)
  47. for (int j=1;j<=m;++j)
  48. {
  49. if (i<n&&ans.a[i][j]!=ans.a[i+1][j]) ++ans.tot;
  50. if (j<m&&ans.a[i][j]!=ans.a[i][j+1]) ++ans.tot;
  51. }
  52. SA(1);SA(1);SA(1);
  53. for (int i=1;i<=n;++i,puts(""))
  54. for (int j=1;j<=m;++j)
  55. printf("%d ",ans.a[i][j]);
  56. return 0;
  57. }

[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. elk安装配置

    ELK介绍   官网https://www.elastic.co/cn/ 中文指南https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/det ...

  2. 用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS

    HTTP 协议是不加密传输数据的,也就是用户跟你的网站之间传递数据有可能在途中被截获,破解传递的真实内容,所以使用不加密的 HTTP 的网站是不太安全的.所以, Google 的 Chrome 浏览器 ...

  3. 如何知道你的linux是什么时候安装的

    在安装系统时,每个分区下都会有一个 lost+found,而且这个目录的创建时间是和该分区创建的时间一样的.所以如果想知道你的系统是什么时候安装的,只需要看这个目录的创建时间即可. 通常情况下,我们分 ...

  4. Filezilla Server日志文件

    Filezilla Server版本:0.9.41. Filezilla Server日志文件在软件安装目录下Logs目录下. 但Filezilla Server默认不开始日志记录,如何开启filez ...

  5. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  6. 腾讯云微计算实践:从Serverless说起,谈谈边缘计算的未来

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:黄文俊,腾讯云高级产品经理,曾经历过企业级存储.企业级容器平台等产品的架构与开发,对容器.微服务.无服务器.DevOps等都有浓厚兴趣. ...

  7. 用Eclipse Maven 创建 Web 3.0 项目问题 正确的处理步骤

    在Eclipse 安装好Maven插件后,创建Maven webapp项目,在工程 properties -> project facets 界面中将 Dynamic Web Module 修改 ...

  8. 用一个简单的例子比较SVM,MARS以及BRUTO(R语言)

    背景重述 本文是ESL: 12.3 支持向量机和核中表12.2的重现过程.具体问题如下: 在两个类别中产生100个观测值.第一类有4个标准正态独立特征\(X_1,X_2,X_3,X_4\).第二类也有 ...

  9. 1.2 decimal模块

    >>> 0.1 + 0.1 +0.1 == 0.3 False >>> >>> print(0.1 + 0.1 + 0.1) 0.30000000 ...

  10. hdu1496 打表

    通常可以想到直接四个for枚举,但是会超时.就算只用三个for也很危险.可以用打表的方法将时间复杂度降到O(n^2),注意到x1,x2,x3,x4的取值区间是关于零对称的,因此可以只考虑正整数部分,洗 ...