返回首页

算法竞赛:对拍

2026-09-06 2026-09-08 学习 学习
623 字 3 分钟
算法竞赛:对拍
目录

对拍的形式就是就是通过 与暴力算法 去验证自己的算法的可行性,是否存在边界问题没有考虑,找出 Hack 数据

对拍#

1. 文件结构#

duipai/
├── gen.cpp
├── std.cpp
├── solve.cpp
└── duipai.cpp

2. 数据生成器#

2.1 生成整数#

#include <bits/stdc++.h>
using namespace std;
mt19937 rng(
chrono::steady_clock::now().time_since_epoch().count()
);
int rnd(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
int main() {
int n = rnd(1, 10);
cout << n << endl;
for (int i = 1; i <= n; i++) {
cout << rnd(-100, 100) << " ";
}
cout << endl;
return 0;
}

2.2 生成浮点数#

#include <bits/stdc++.h>
using namespace std;
mt19937 rng(
chrono::steady_clock::now().time_since_epoch().count()
);
double rnd(double l, double r) {
return uniform_real_distribution<double>(l, r)(rng);
}
int main() {
int n = 10;
cout << n << endl;
cout << fixed << setprecision(6);
for (int i = 1; i <= n; i++) {
cout << rnd(-100.0, 100.0) << " ";
}
cout << endl;
return 0;
}

2.3 同时生成整数和浮点数#

#include <bits/stdc++.h>
using namespace std;
mt19937 rng(
chrono::steady_clock::now().time_since_epoch().count()
);
int rndInt(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
double rndDouble(double l, double r) {
return uniform_real_distribution<double>(l, r)(rng);
}
int main() {
int n = rndInt(1, 10);
cout << n << endl;
for (int i = 1; i <= n; i++) {
cout << rndInt(-100, 100) << " ";
}
cout << endl;
cout << fixed << setprecision(6);
for (int i = 1; i <= n; i++) {
cout << rndDouble(-100.0, 100.0) << " ";
}
cout << endl;
return 0;
}

3. Windows 对拍#

3.1 编译#

Terminal window
g++ gen.cpp -std=c++17 -O2 -o gen.exe
g++ std.cpp -std=c++17 -O2 -o std.exe
g++ solve.cpp -std=c++17 -O2 -o solve.exe
g++ duipai.cpp -std=c++17 -O2 -o duipai.exe

3.2 duipai.cpp#

#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt = 0;
while (true) {
cnt++;
system("gen.exe > test.in");
system("std.exe < test.in > std.out");
system("solve.exe < test.in > solve.out");
if (system("fc std.out solve.out > nul")) {
cout << "\nWrong Answer!" << endl;
cout << "Test: " << cnt << endl;
cout << "\nInput:" << endl;
system("type test.in");
cout << "\nStandard Output:" << endl;
system("type std.out");
cout << "\nYour Output:" << endl;
system("type solve.out");
break;
}
cout << "Accepted: " << cnt << "\r";
}
return 0;
}

运行:

Terminal window
duipai.exe

4. Linux 对拍#

4.1 编译#

Terminal window
g++ gen.cpp -std=c++17 -O2 -o gen
g++ std.cpp -std=c++17 -O2 -o std
g++ solve.cpp -std=c++17 -O2 -o solve
g++ duipai.cpp -std=c++17 -O2 -o duipai

4.2 duipai.cpp#

#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt = 0;
while (true) {
cnt++;
system("./gen > test.in");
system("./std < test.in > std.out");
system("./solve < test.in > solve.out");
if (system("diff std.out solve.out > /dev/null")) {
cout << "\nWrong Answer!" << endl;
cout << "Test: " << cnt << endl;
cout << "\nInput:" << endl;
system("cat test.in");
cout << "\nStandard Output:" << endl;
system("cat std.out");
cout << "\nYour Output:" << endl;
system("cat solve.out");
break;
}
cout << "Accepted: " << cnt << "\r";
}
return 0;
}

运行:

Terminal window
./duipai

5. 常用命令#

Windows#

Terminal window
gen.exe > test.in
std.exe < test.in > std.out
solve.exe < test.in > solve.out
fc std.out solve.out

Linux#

Terminal window
./gen > test.in
./std < test.in > std.out
./solve < test.in > solve.out
diff std.out solve.out

6. 浮点数对拍#

浮点数不要直接比较文件,使用误差判断:

double a, b;
if (fabs(a - b) > 1e-6) {
cout << "Wrong Answer" << endl;
}

常用:

bool equal(double a, double b) {
return fabs(a - b) <= 1e-6;
}

评论