跳到正文

算法·理论

大众化的传说级算法,不看白不看

dreamnotfounddd约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年8月18日
收录
2026年11月23日
更新
2026年11月23日
篇幅
约 3 分钟

版本

  1. v1当前

(2026.10.31) 由于原来的开头写的太诡异了,因此删除了开头

对拍

对拍的主要原理是:通过Windows和Linux的电脑指令进行又快又好的随机样例调试(注意⚠️,下面的代码不适用于苹果Mac),并且是一位清华巨佬推荐给我的,非常之好用。

cmp.bat:

代码
:loop
mkdt.exe
a.exe > a.out
b.exe > b.out
fc /W a.out b.out
if errorlevel 1 goto end
goto loop
:end
pause

mkdt.cpp:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
const int mod=998244353;
inline int getn(int a,int b){return (rand()*32768+rand())%(b-a+1)+a;}
int main(){
	FILE *dsk = fopen("seed.txt","r");unsigned int sd,ccc;fscanf(dsk,"%u%u",&sd,&ccc);
	FILE *gsk = fopen("seed.txt","w");sd+=23333,ccc+=1;fprintf(gsk,"%u\n%u\n",sd,ccc);
	freopen("data.in","w",stdout);srand((unsigned int )time(0)+sd);
	//cout all the data 
	return 0;
}

然后在a.cpp和b.cpp里面分别写正解和暴力,然后从data.in里面读入,输出到a.out和b.out

在运行的时候只要双击cmp.bat即可

但是容易发现,对拍这个算法对于大模拟是没有效果的(除非你把另一个人的正解写到b.cpp里面,但是在考场上真的有正解给你看嘛?)

卡时小技巧

C++
if(clock() / (1.0* CLOCKS_PER_SEC) >= 0.95){
  cout << ans << "\n";
  return 0;
}

并且这个方法小数据会AC,但大数据就看命了

如何测程序运行时间(2026.10.30加更)

C++
clock_t start_time = clock();

//主函数

clock_t end_time = clock();
double seconds = 1.0*(end_time-start_time)/CLOCKS_PER_SEC;
//fprintf(stderr,"TIME : %.3f s\n",seconds);
printf("Time : %.3f s\n",seconds);
原文