对拍常见技巧

· · 个人记录

本文整理 OI 赛制常用对拍技巧。

常见控制台命令

fc

用法:fc <参数> <文件 1> <文件 2>。

比较两个文件,会给出不同的位置或者返回完全相同。可能有换行或空格的坑。

常用参数 /N,会在找到不同时显示行号。

示例:fc /N a.out a.ans。

流输入输出

如果你在代码里面没有写 freopen,可以在控制台运行 .exe 的时候用这个(主要方便写脚本)

用法:my.exe <in.in >out.out,其中文件名可以改。这会重定向流输入输出。(或许对 printf 之类的没什么作用?没测试过)

常见对拍代码技巧

mt19937 和 mt19937_64

头文件好像是 <mt19937>,反正万能头里面有。至少要 c++14。

常用的初始化方式:

mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

用法:rng(),返回一个随机数,范围好像是 [0,INT_MAX]。如果是 mt19937_64 就是 long long 范围。

int rnd(int l, int r){ // 返回 [l,r] 的正整数
  return rng() % (r - l + 1) + l;
}

时间

其实就是上面的 std::chrono::steady_clock::now().time_since_epoch().count()。返回当前时间(long long),精确到纳秒。实际精度取决于 CPU 速度,并不一定是 1 \text{ ns}。1 \text{ ms}=10^6 \text{ ns}。 常用:

ll st = std::chrono::steady_clock::now().time_since_epoch().count();
// do something...
ll ed = std::chrono::steady_clock::now().time_since_epoch().count();

听 AI 说不能直接除以 10^6,要这样算毫秒:

chrono::milliseconds dur = chrono::duration_cast<chrono::milliseconds>(ed - st);
ll ms = dur.count();

system()

用法:system(string cmd);。执行一段命令行代码。配合前面所说的命令可以实现自动化对拍。

自动测试大样例

假设大样例格式为 sample(x).in/ans,其中 (x) 是一个不带前导 0 的整数,表示测试点编号,从 1 开始。

#include <bits/stdc++.h>
#include <chrono>
#define fi first
#define se second
#define pii pair<int, int>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pb push_back
#define mkp make_pair
#define debug(x) cerr << #x << ": " << (x) << "\n"
#define ll long long
#define uint unsigned int
#define ull unsigned long long
using namespace std;

string prob = "candy";
string sam = "sample";

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int T = 10;
    for(int C = 1; C <= T; C++){
        string file = sam + to_string(C);
        chrono::steady_clock::time_point st = chrono::steady_clock::now();
        system((prob + ".exe <" + file + ".in >" + file + ".out").c_str());
        chrono::steady_clock::time_point ed = chrono::steady_clock::now();
        int ret = system(("fc /N " + file + ".out " + file + ".ans").c_str());
        chrono::milliseconds dur = chrono::duration_cast<chrono::milliseconds>(ed - st);
        ll ms = dur.count();
        cout << "Case #" << C << ":\n";
        cout << "Time used: " << ms << "ms\n";
        if(ret != 0) cout << "WA on test case #" << C << "\n";
        else cout << "AC on test case #" << C << "\n";
    }
}

自动对拍

你需要实现:

#include <bits/stdc++.h>
#include <chrono>
#define fi first
#define se second
#define pii pair<int, int>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pb push_back
#define mkp make_pair
#define debug(x) cerr << #x << ": " << (x) << "\n"
#define ll long long
#define uint unsigned int
#define ull unsigned long long
using namespace std;

string prob = "candy";
string sam = "sample";

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int T = 1000;
    for(int C = 1; C <= T; C++){
        system(("gen.exe >" + sam + ".out").c_str());
        system(("baoli.exe <" + sam + ".in >" + sam + ".ans").c_str());
        chrono::steady_clock::time_point st = chrono::steady_clock::now();
        system((prob + ".exe <" + sam + ".in >" + sam + ".out").c_str());
        chrono::steady_clock::time_point ed = chrono::steady_clock::now();
        int ret = system(("fc /N " + sam + ".out " + sam + ".ans").c_str());
        chrono::milliseconds dur = chrono::duration_cast<chrono::milliseconds>(ed - st);
        ll ms = dur.count();
        cout << "Case #" << C << ":\n";
        cout << "Time used: " << ms << "ms\n";
        if(ret != 0){
            cout << "WA on test case #" << C << "\n";
            return 0;
        }
        else cout << "AC on test case #" << C << "\n";
    }
}