upt:2022年10月20日,新增模拟退火、爬山法。
快 CSP 了,想学生成数据。以后学到模拟退火等随机化算法也会一起写在这里。
从 rand 说起
#include <cstdlib>
#include <ctime>
//...
srand(time(NULL));//使用当前的时间作为随机种子
int x=rand();以上的代码会得到一个 之间的数。
//rand 生成随机数,以下函数对于 int 范围的 n 都能正常运行
//随机数据生成
#include <cstdlib>
#include <ctime>
int random(int n)
{
return 1ll*rand()*rand()%n;
}
int main()
{
srand((unsigned)time(0));
//···
}
缺点:随机周期短,慢
mt19937
由于 CCF 大部分开放 C++14 标准,可以使用这个高质量还快的随机函数。
#include <random>
#include <ctime>
using namespace std;
//...
mt19937 rnd(time(0));
mt19937_64 long_rnd(time(0));前者是 unsigned int 范围,后者是 unsigned long long 范围。
两者都是一个类,用来定义随机数函数。调用直接像rand一样即可。
随机实数:先产生一个较大的随机整数,再除以 的次幂。
随机负数:值域平移之后减掉 。
shuffle
有个叫做 random_shuffle 的东西,以后不让用了,这两个东西都在 algorithm 头文件里面。
调用 shuffle(a+1,a+1+n,rnd); ,其中 rnd 是自定义的随机数生成器。
随机数据生成
//随机生成整数序列
int n=rnd()%100000+1;
int m=1e9;
for(int i=1;i<=n;i++) a[i]=rnd()%(2*m+1)-m;
//随机生成区间列
for(int i=1;i<=m;i++)
{
int l=rnd()%n+1,r=rnd()%n+1;
if(l > r) l^=r^=l^=r;
printf("%d %d\n",l,r);
}
//随机生成树
for(int i=2;i<=n;i++)
{
//从 [2,n] 之间的每个点向前面的点随机连一条边
int fa=rnd()%(i-1)+1;
int val=rnd()%1000000000+1;
printf("%d %d %d\n",fa,i,val);
}
树的数据还可以特殊构造,链形,菊花,蒲公英(缝合怪)数据都是经常让算法复杂度退化的特殊数据。
由于考场上我应该没有时间生成图,所以这里先咕着,有空了来搞。
对拍
造数据就是为了这个。
我从 wt giegie 那里学的,参考 lyd 蓝书和洛谷日报。
注意对拍的时候不要开文件输入输出。
将对拍器和数据生成器,你写的暴力和正解放在同一个文件夹里面,就可以开始对拍了。
对拍跑一般多组小数据,如果各种数据都可以通过,基本可以拿到满分。
如果是平时调代码用,也可以生成大数据和其他人的 AC 代码进行对拍。
看文件名可以看出来啥是啥,注意对拍时注释掉文件输入输出。
对拍器长这样。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
double s,myt,stdt;
for(int i=1;i<=10000;i++)
{
system("data.exe > test.in");//测试的数据输入 test.in
s=clock();//获取当前时间
system("test.exe < test.in > test.out");//将 test.in 中的数据输入 test.exe , 并将运行结果输入 test.out
myt=clock();
system("std.exe < test.in > std.out");
stdt=clock();
//同上
if(system("fc /W test.out std.out"))//fc 比较两者是否相同,不相同返回真。/W 过滤多余空格和换行
{
printf("Wrong Answer, 测试点 #%d, 用时 %.0lfms, std用时 %.0lfms",i,myt-s,stdt-myt);
return 0;
}//时间的单位在 Windows 下是毫秒,需要换算自己手算就好了
else printf("Accepted, 测试点 #%d, 用时 %.0lfms, std用时 %.0lfms\n",i,myt-s,stdt-myt);
}
return 0;
}参考&学习 资料
- 《算法竞赛进阶指南》——李煜东
- 随机函数——OI Wiki
- 深海中的STL—mt19937——attack
- [洛谷日报#86]OIer 必知的编程技巧
模拟退火
在 Acwing 学习之后做一下总结。
例题 UVA10228 A Star not a Tree?
题意是求多边形费马点坐标,即求一个点到其他点的距离和最小。
对于坐标轴每一个点到顶点的距离和是一定的,可以抽象为一个函数。由凸函数的性质(凸性相同的函数相加仍为凸性一样的凸函数)可知这应该是一个单谷函数,在二维的坐标轴中可以使用三分套三分的办法来求。
但是我不会数学,或者考场上猜不出来结论,那么我们可以使用模拟退火算法来解决这样的问题。
感觉说的不是人话,动图和技巧还是不错的。
那么,模拟退火是什么呢?
对于一个比较有连续性的函数,我们需要求函数上的极值。可以通过随机一个点,计算该点的函数值,如果该点比当前点更优,我们就跳过去,否则我们以一定的概率跳过去。本质是对搜索算法的一种改进。
如果直接搜索的话我们难以找到一个搜索的方向,也就和枚举差不多了。
模拟退火为我们提供了这样的可能,在不断降温的过程中,我们会找到一个峰值。而为了避免找到的是局部最优,有一定的概率我们会舍弃当前看似更优的状态。同时,我们进行多次模拟退火,可以大大提升找到最优解的概率。
例题的代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <ctime>
using namespace std;
const int N=110;
typedef double db;
typedef pair <db,db> PDD;
int n;
PDD a[N];
db ans;
inline db rnd(db l,db r) {
return (db)rand()/RAND_MAX*(r-l)+l;
}
//计算距离
inline db getdis(PDD x,PDD y) {
return sqrt((x.first-y.first)*(x.first-y.first)+(x.second-y.second)*(x.second-y.second));
}
//计算该点函数值
db calc(PDD p)
{
db res=0;
for(int i=0;i<n;i++) res+=getdis(p,a[i]);
ans=min(ans,res);
return res;
}
inline void simulate_anneal()
{
PDD cur(rnd(0,10000),rnd(0,10000));
for(db t=1e4;t>1e-4;t*=0.99)//初温,降温系数。这里的参数是要调的
{
PDD np(rnd(cur.first-t,cur.first+t),rnd(cur.second-t,cur.second+t));
db delta=calc(np)-calc(cur);
if(exp(-delta/t) > rnd(0,1)) cur=np;//这个表达式怎么写取决于具体问题
}
return ;
}
int main()
{
// freopen("work.in","r",stdin); freopen("work.out","w",stdout);
int T;
scanf("%d",&T);
for(int k=1;k<=T;k++)
{
ans=1e8;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].first,&a[i].second);
for(int i=0;i<100;i++) simulate_anneal();
printf("%.0lf\n",ans);
if(k^T) printf("\n");
}
// fclose(stdin); fclose(stdout);
return 0;
}P1337 [JSOI2004] 平衡点 / 吊打XXX 带权,调参比较难调。
模拟退火确定顺序
模拟退火主要应用于计算几何,确定顺序。在确定了随机顺序后再加上 dp ,贪心等等计算函数值。
例题 P4044 [AHOI2014/JSOI2014]保龄球
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;
const int N=60;
typedef pair<int,int> PII;
typedef double db;
int n,m,ans;
PII p[N];
inline int calc()
{
int res=0;
for(int i=0;i<m;i++)
{
res+=p[i].first+p[i].second;
if(i < n)
{
if(p[i].first == 10) res+=p[i+1].first+p[i+1].second;
else if(p[i].first+p[i].second == 10) res+=p[i+1].first;
}
}
ans=max(ans,res);
return res;
}
inline void simulate_anneal()
{
for(db t=1e4;t>1e-4;t*=0.99)
{
int a=rand()%m,b=rand()%m;
int x=calc(),y;
swap(p[a],p[b]);
if(n+(p[n-1].first == 10) == m)
{
y=calc();
int delta=y-x;
if(exp(delta/t) < (double)rand()/RAND_MAX) swap(p[a],p[b]);
}
else swap(p[a],p[b]);
}
}
int main()
{
// freopen("work.in","r",stdin); freopen("work.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d%d",&p[i].first,&p[i].second);
if(p[n-1].first == 10) scanf("%d%d",&p[n].first,&p[n].second);
m=n+(p[n-1].first == 10);
while((double)clock()/CLOCKS_PER_SEC <= 0.8) simulate_anneal();
printf("%d\n",ans);
// fclose(stdin); fclose(stdout);
return 0;
}为了提高退火正确率,可以运用卡时的技巧,但是注意不要卡得太死,万一最后一遍跑得很慢就可能 TLE 。
什么情况下用
函数值不会一下子变化太大,且无法找到合适的算法,就考虑下能不能用。
爬山法
用于求单峰(单谷)函数极值。
为啥不写三分?维数过高就不好做了。
code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=25;
typedef double db;
int n;
db a[N];
db calc(db x)
{
db res=a[0],xx=1;
for(int i=1;i<=n;i++) {
xx*=x;
res+=xx*a[i];
}
return res;
}
int main()
{
// freopen("climb.in","r",stdin); freopen("climb.out","w",stdout);
db l,r;
scanf("%d%lf%lf",&n,&l,&r);
for(int i=n;~i;i--) scanf("%lf",&a[i]);
db ans=l;
for(db t=10,ll,rr;t>1e-8;t*=0.99)
{
ll=ans-t; rr=ans+t;
if(rr <= r && calc(rr) > calc(ans)) ans=rr;
if(ll >= l && calc(ll) > calc(ans)) ans=ll;
}
printf("%.5lf\n",ans);
// fclose(stdin); fclose(stdout);
return 0;
}代码难度低,容易调。并不常用
之前在机房一些想到的没写下来,现在忘了,想到了再补充。
