整理了一份竞赛常用代码模板,考试前打印出来看一遍。
一、快读快写模板
inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < "0" || ch > "9") { if (ch == "-") f = -1; ch = getchar(); }
while (ch >= "0" && ch <= "9") { x = x * 10 + ch - "0"; ch = getchar(); }
return x * f;
}
inline void write(int x) {
if (x < 0) putchar("-"), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + "0");
}
二、常用宏定义
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
⚠️ 注意:慎用
#define int long long,可能会导致 MLE 或 TLE!
三、对拍程序
- 写一个暴力保证正确的程序(bf.cpp)
- 写一个随机数据生成器(gen.cpp)
- 用批处理/Shell 脚本循环运行,比较输出
四、调试技巧
#ifdef DEBUG
#define debug(x) cerr << #x << " = " << x << endl
#else
#define debug(x)
#endif
用
#define DEBUG 或 -DDEBUG 编译时开启调试输出。
五、手动开栈
// Windows: 编译时添加 -Wl,--stack=16777216
// Linux: ulimit -s unlimited
用于 DFS 递归层数很深的情况。
六、常用算法模板速查
- 并查集、树状数组、线段树
- KMP、哈希、Trie 树
- 快速幂、逆元、GCD
- Dijkstra、SPFA、Tarjan
- 背包、LIS、LCS
建议考试前打印出来,方便快速查阅。
暂无评论,快来抢沙发吧~ 🛋️