ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

算法札记:jiangly大神代码风格总结

算法札记:jiangly大神代码风格总结

Jiangly 代码风格特点总结

Jiangly 的代码风格以“规范、简洁、工程化”著称。在算法竞赛高压环境下,这种风格能显著降低调试成本、减少低级失误,因此很适合作为学习标杆1

1. 基础规范:从源头减少错误

  • 缩进与空格:坚持使用 4 个空格缩进;运算符两侧保留空格;逗号、分号后接空格。这些细节让代码层次分明,扫一眼就能看清结构2

  • 命名:变量名通常很短,但含义一致,例如nmxyrescurpre;常用类型别名如using i64 = long long;

  • 头文件与命名空间:通常直接使用<bits/stdc++.h>,并尽量减少不必要的全局using namespace std;,而是使用std::前缀或显式类型别名,避免命名冲突2

2. 类型与模板:避免溢出、加速开发

  • 常用using i64 = long long;using u64 = unsigned long long;等别名,从源头规避 int 溢出问题。

  • 自带经过验证的模板:快读快写、ModInt、组合数、线段树、树状数组、数论函数等。这些模板并非堆砌,而是针对 Codeforces / AtCoder 等平台的常用场景打磨而成3

  • 喜欢使用现代 C++ 特性,例如auto声明、lambda 递归、泛型函数,减少重复代码。

3. 结构组织:平铺直叙,减少嵌套

  • 常把单组处理逻辑放在solve()函数中,主流程清晰:读入、计算、输出。

  • 会主动把重复逻辑抽成函数或结构体,但不过度设计;选择最优数据结构后直接实现,让“逻辑流”尽量线性化。

  • 减小嵌套深度:能提前return就提前return,能用循环/函数替代多层 if,降低心智负担。

4. 输入输出与常数优化

  • 使用std::ios::sync_with_stdio(false);std::cin.tie(nullptr);关闭同步,但不会过度使用手写 IO,除非数据量极大。

  • 使用constexprconst定义常量,避免魔法数字;数组/容器大小用明确的nN表示。

5. 对普通选手的启发

  • 先模仿:把缩进、空格、命名习惯固定下来,代码可读性会立刻提升。

  • 再内化:理解每块模板背后的原理,最终形成“写好代码的通用原则”,而不是死记硬背。

总之,Jiangly 的风格不是单纯“短”,而是“少而准”:每行代码、每个命名、每个抽象都有明确目的,这使他能在高难度题目中保持稳定输出。

如果希望逐字参考 Jiangly 的真实提交,可以直接在 Codeforces / AtCoder 的 standings 页面点开他的提交记录。下面整理几个符合他常见风格的代码实例,重点体现规范、简洁、少嵌套的特点1

1. 基础骨架

Jiangly 的代码通常从这几个基础类型别名开始:

C++

#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using f64 = double; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); // ... return 0; }

这种写法能统一整份代码里的整数类型,减少int溢出问题2

2. 并查集 DSU

他的许多图论、连通性题目会直接使用简洁的 DSU 封装:

C++

struct DSU { std::vector<int> f, siz; DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); } int find(int x) { while (f[x] != x) { x = f[x] = f[f[x]]; } return x; } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) std::swap(x, y); f[y] = x; siz[x] += siz[y]; return true; } };

调用时逻辑非常直白:

C++

void solve() { int n, m; std::cin >> n >> m; DSU dsu(n); for (int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; --u; --v; dsu.unite(u, v); } int ans = 0; for (int i = 0; i < n; i++) { if (dsu.find(i) == i) ans++; } std::cout << ans << '\n'; }

尽量让主流程平铺直叙,减少不必要的嵌套2

3. 完整示例:单点修改 + 区间最大子段和

这是典型的“数据结构 + 函数式合并”写法,也是 Jiangly 常用的风格。

C++

#include <bits/stdc++.h> using i64 = long long; struct Info { i64 sum = 0; i64 maxPrefix = 0; i64 maxSuffix = 0; i64 maxSubarray = 0; }; Info operator+(const Info &a, const Info &b) { Info c; c.sum = a.sum + b.sum; c.maxPrefix = std::max(a.maxPrefix, a.sum + b.maxPrefix); c.maxSuffix = std::max(b.maxSuffix, b.sum + a.maxSuffix); c.maxSubarray = std::max({a.maxSubarray, b.maxSubarray, a.maxSuffix + b.maxPrefix}); return c; } template<class Info> struct SegmentTree { int n; std::vector<Info> tree; SegmentTree(int n) : n(n), tree(4 << std::__lg(n)) {} void pull(int p) { tree[p] = tree[p << 1] + tree[p << 1 | 1]; } void modify(int p, int l, int r, int x, const Info &v) { if (r - l == 1) { tree[p] = v; return; } int m = (l + r) / 2; if (x < m) modify(p << 1, l, m, x, v); else modify(p << 1 | 1, m, r, x, v); pull(p); } Info query(int p, int l, int r, int x, int y) { if (l >= y || r <= x) return Info{}; if (l >= x && r <= y) return tree[p]; int m = (l + r) / 2; return query(p << 1, l, m, x, y) + query(p << 1 | 1, m, r, x, y); } void modify(int x, const Info &v) { modify(1, 0, n, x, v); } Info query(int x, int y) { return query(1, 0, n, x, y); } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; std::cin >> n >> q; SegmentTree<Info> seg(n); for (int i = 0; i < n; i++) { i64 x; std::cin >> x; seg.modify(i, {x, std::max(0LL, x), std::max(0LL, x), std::max(0LL, x)}); } while (q--) { int type; std::cin >> type; if (type == 1) { int pos; i64 x; std::cin >> pos >> x; --pos; seg.modify(pos, {x, std::max(0LL, x), std::max(0LL, x), std::max(0LL, x)}); } else { int l, r; std::cin >> l >> r; --l; std::cout << seg.query(l, r).maxSubarray << '\n'; } } return 0; }

4. ModInt 快速幂

在组合数学和概率题中,这种模板也经常出现:

C++

template<int MOD> struct ModInt { int v; ModInt(int v = 0) : v(v) {} ModInt &operator+=(const ModInt &rhs) { v += rhs.v; if (v >= MOD) v -= MOD; return *this; } ModInt &operator*=(const ModInt &rhs) { v = 1LL * v * rhs.v % MOD; return *this; } friend ModInt power(ModInt a, long long b) { ModInt res = 1; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } };

小结

Jiangly 的代码风格并不是“花哨的炫技”,而是用统一规则减少比赛中的低级失误:

  • 固定模板,但不用过多宏定义。

  • std::vector、struct、template 提高可复用性。

  • 让主干solve()尽量线性,能提前return就提前return2

  • 所有复杂结构都封装成易读的接口,使用处只保留业务逻辑1

返回列表