Convert Figma logo to code with AI

EndlessCheng logocodeforces-go

算法竞赛模板库 by 灵茶山艾府 💭💡🎈

4,915
540
4,915
7

Top Related Projects

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

128,386

A curated list of awesome Go frameworks, libraries and software

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

:memo: LeetCode of algorithms with golang solution(updating).

【未来服务器端编程语言】最全空降golang资料补给包(满血战斗),包含文章,书籍,作者论文,理论分析,开源框架,云原生,大佬视频,大厂实战分享ppt

Quick Overview

EndlessCheng/codeforces-go is a GitHub repository containing Go solutions for Codeforces problems. It provides a comprehensive collection of algorithmic solutions implemented in Go, serving as a valuable resource for competitive programmers and those preparing for coding interviews.

Pros

  • Extensive collection of Go solutions for Codeforces problems
  • Well-organized structure with solutions categorized by contest and problem number
  • Includes explanations and comments for many solutions, aiding in understanding
  • Regularly updated with new solutions and improvements

Cons

  • May not cover all Codeforces problems
  • Solutions might not always be the most optimal or efficient
  • Primarily focused on Go, limiting usefulness for users of other programming languages
  • Reliance on external solutions may hinder personal problem-solving skills development

Code Examples

  1. Binary Search implementation:
func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}
  1. Depth-First Search (DFS) on a graph:
func dfs(graph map[int][]int, start int, visited map[int]bool) {
    visited[start] = true
    fmt.Printf("%d ", start)
    for _, neighbor := range graph[start] {
        if !visited[neighbor] {
            dfs(graph, neighbor, visited)
        }
    }
}
  1. Sieve of Eratosthenes for finding prime numbers:
func sieveOfEratosthenes(n int) []bool {
    isPrime := make([]bool, n+1)
    for i := 2; i <= n; i++ {
        isPrime[i] = true
    }
    for i := 2; i*i <= n; i++ {
        if isPrime[i] {
            for j := i * i; j <= n; j += i {
                isPrime[j] = false
            }
        }
    }
    return isPrime
}

Getting Started

To use the solutions from this repository:

  1. Clone the repository:

    git clone https://github.com/EndlessCheng/codeforces-go.git
    
  2. Navigate to the desired problem's solution:

    cd codeforces-go/problemset
    
  3. Run the Go file for the specific problem:

    go run problem_number.go
    

Remember to replace problem_number with the actual problem number you're interested in.

Competitor Comparisons

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

Pros of hello-algorithm

  • More comprehensive coverage of algorithms and data structures
  • Includes visual explanations and diagrams for better understanding
  • Offers content in multiple languages (Chinese and English)

Cons of hello-algorithm

  • Less focused on competitive programming compared to codeforces-go
  • May not have as many advanced or specialized algorithms
  • Updates less frequently than codeforces-go

Code Comparison

hello-algorithm (Python):

def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

codeforces-go (Go):

func binarySearch(a []int, x int) int {
    l, r := 0, len(a)
    for l < r {
        m := (l + r) >> 1
        if a[m] >= x {
            r = m
        } else {
            l = m + 1
        }
    }
    return l
}

Both repositories provide implementations of common algorithms, but codeforces-go focuses more on competitive programming solutions, while hello-algorithm offers a broader range of algorithms and data structures with explanations. The code comparison shows similar implementations of binary search in different languages, reflecting the repositories' focus on algorithm implementations.

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Pros of LeetCode-Go

  • More comprehensive coverage of LeetCode problems
  • Better organization with problems categorized by difficulty and topics
  • Includes detailed explanations and time/space complexity analyses

Cons of LeetCode-Go

  • Focuses solely on LeetCode, limiting its scope compared to codeforces-go
  • May not include as many advanced algorithmic techniques used in competitive programming

Code Comparison

LeetCode-Go:

func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i, num := range nums {
        if j, ok := m[target-num]; ok {
            return []int{j, i}
        }
        m[num] = i
    }
    return nil
}

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    ans := 0
    for i := 0; i < n; i++ {
        ans += abs(a[i] - (i + 1))
    }
    return ans
}

The code examples showcase different problem-solving approaches. LeetCode-Go focuses on common interview questions, while codeforces-go addresses more competitive programming-style problems. Both repositories provide valuable resources for Go developers looking to improve their algorithmic skills, with LeetCode-Go being more suitable for interview preparation and codeforces-go for competitive programming practice.

128,386

A curated list of awesome Go frameworks, libraries and software

Pros of awesome-go

  • Comprehensive curated list of Go libraries, tools, and resources
  • Regularly updated with community contributions
  • Well-organized into categories for easy navigation

Cons of awesome-go

  • Not focused on competitive programming or algorithm solutions
  • Lacks actual code implementations or examples
  • May be overwhelming for beginners due to the sheer volume of resources

Code comparison

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    ans := 0
    for i := 0; i < n; i++ {
        ans += abs(a[i] - (i + 1))
    }
    return ans
}

awesome-go:

No direct code comparison available, as awesome-go is a curated list of resources rather than a code repository.

Summary

codeforces-go is a repository focused on competitive programming solutions in Go, providing implementations for various algorithms and data structures. On the other hand, awesome-go is a curated list of Go libraries, tools, and resources, serving as a comprehensive reference for Go developers. While codeforces-go offers practical code examples for algorithmic problems, awesome-go provides a broader overview of the Go ecosystem without specific code implementations.

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

Pros of algorithm-pattern

  • Focuses on algorithm patterns and provides a structured learning approach
  • Covers a wide range of common algorithms and data structures
  • Includes explanations and examples in multiple programming languages

Cons of algorithm-pattern

  • Less comprehensive in terms of problem-solving techniques specific to competitive programming
  • May not be as frequently updated with new problems and solutions
  • Lacks the extensive collection of Codeforces problem solutions

Code Comparison

algorithm-pattern (Go implementation of binary search):

func binarySearch(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right-left)>>1
        if nums[mid] == target {
            return mid
        } else if nums[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

codeforces-go (Binary search implementation from a Codeforces problem):

func f(m int) bool {
    // Problem-specific condition
}

func binarySearch() int {
    l, r := 0, 1e9
    for l < r {
        m := (l + r) >> 1
        if f(m) {
            r = m
        } else {
            l = m + 1
        }
    }
    return l
}

The code comparison shows that codeforces-go focuses more on competitive programming-specific implementations, while algorithm-pattern provides a more general approach to algorithms.

:memo: LeetCode of algorithms with golang solution(updating).

Pros of awesome-golang-algorithm

  • Broader coverage of algorithms and data structures beyond competitive programming
  • More organized structure with categorized algorithms
  • Includes explanations and comments for better understanding

Cons of awesome-golang-algorithm

  • Less focused on specific competitive programming platforms
  • May not cover as many advanced or niche algorithms used in competitions
  • Updates might be less frequent compared to codeforces-go

Code Comparison

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    return a[n/2]
}

awesome-golang-algorithm:

func BinarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

The codeforces-go example shows a concise solution for a specific problem, while the awesome-golang-algorithm example demonstrates a more general-purpose implementation of a common algorithm with detailed steps.

【未来服务器端编程语言】最全空降golang资料补给包(满血战斗),包含文章,书籍,作者论文,理论分析,开源框架,云原生,大佬视频,大厂实战分享ppt

Pros of Introduction-to-Golang

  • Comprehensive learning resource for Go beginners
  • Covers a wide range of Go topics and concepts
  • Includes practical examples and exercises

Cons of Introduction-to-Golang

  • Less focused on competitive programming
  • May not provide advanced algorithmic solutions
  • Updates less frequently compared to codeforces-go

Code Comparison

Introduction-to-Golang (basic Go syntax):

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

codeforces-go (competitive programming solution):

package main

import (
    "bufio"
    "os"
)

func solve(in *bufio.Reader, out *bufio.Writer) {
    // Problem-specific solution
}

The Introduction-to-Golang repository is better suited for beginners learning Go from scratch, offering a structured approach to understanding the language. On the other hand, codeforces-go is tailored for competitive programmers, providing solutions to Codeforces problems and focusing on algorithmic challenges. While Introduction-to-Golang covers a broader range of topics, codeforces-go offers more specialized content for algorithm enthusiasts and those preparing for coding competitions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

算法竞赛模板库 by 灵茶山艾府 💭💡🎈

算法 Algorithm

由于算法知识点繁杂,将自己学习到的算法、做过的题目分类整理好是有必要的。

一个算法模板应当涵盖以下几点:

  • 对该算法的基本介绍(核心思想、复杂度等)
  • 参考链接或书籍章节(讲得比较好的资料)
  • 模板代码(代码注释、使用说明)
  • 模板补充(常见题型中的额外代码、建模技巧等)
  • 相关题目(模板题、经典题、思维转换题等)

算法目录

不了解 Go?快速入门教程

  • 集合论与位运算
  • 数据结构
  • 字符串 strings.go
  • 数学
    • 数论 math.go
      • 辗转相除法(最大公因数 GCD)
      • 类欧几里得算法 ∑⌊(ai+b)/m⌋
      • Pollard-Rho 质因数分解算法
      • 埃氏筛(埃拉托斯特尼筛法)
      • 欧拉筛(线性筛)
      • 欧拉函数
      • 原根
      • 扩展 GCD
        • 二元一次不定方程
      • 逆元
        • 线性求逆元
      • 中国剩余定理(CRT)
        • 扩展中国剩余定理
      • 离散对数
      • 大步小步算法(BSGS)
        • 扩展大步小步算法
      • 二次剩余
      • Jacobi 符号
      • N 次剩余
      • 卢卡斯定理
        • 扩展卢卡斯定理
      • 卡特兰数
      • 默慈金数
      • 那罗延数
      • 斯特林数
        • 第一类斯特林数(轮换)
        • 第二类斯特林数(子集)
      • 贝尔数
      • 欧拉数
      • 数论分块(整除分块)
      • 莫比乌斯函数
      • 莫比乌斯反演
        • 互质计数问题
        • GCD 求和问题
      • 杜教筛
    • 组合数学 math_comb.go
      • 常见模型
      • 常用恒等式
      • 容斥原理
    • 快速傅里叶变换 FFT math_fft.go
    • 快速数论变换 NTT math_ntt.go
      • 包含多项式全家桶(求逆、开方等等)
    • 快速沃尔什变换 FWT math_fwt.go
    • 连分数、佩尔方程 math_continued_fraction.go
    • 线性代数 math_matrix.go
      • 矩阵相关运算
      • 高斯消元
      • 行列式
      • 线性基
    • 数值分析 math_numerical_analysis.go
      • 自适应辛普森积分
      • 拉格朗日插值
    • 计算几何 geometry.go
      • 线与点
      • 线与线
      • 圆与点
        • 最小圆覆盖
          • Welzl 随机增量法
        • 固定半径覆盖最多点
      • 圆与线
      • 圆与圆
      • 圆与矩形
      • 最近点对
      • 多边形与点
        • 判断点在凸多边形内 $O(\log n)$
        • 判断点在任意多边形内
          • 转角法(统计绕数)
      • 凸包
      • 最远点对
        • 旋转卡壳
      • 半平面交
    • 博弈论 games.go
      • SG 函数
  • 动态规划 dp.go
    • 背包
      • 0-1 背包
      • 完全背包
      • 多重背包
        • 二进制优化
        • 单调队列优化
        • 同余前缀和优化(求方案数)
      • 分组背包
      • 树上背包(依赖背包)
      • 字典序最小方案
    • 线性 DP
      • 最大子段和
      • LCS
      • LPS
      • LIS
        • 狄尔沃斯定理
      • LCIS
      • 长度为 m 的 LIS 个数
      • 本质不同子序列个数
    • 区间 DP
    • 环形 DP
    • 博弈 DP
    • 概率 DP
    • 期望 DP
    • 状压 DP
      • 全排列 DP
      • 旅行商问题(TSP)
      • 子集 DP
      • 高维前缀和(SOS DP)
      • 插头 DP
    • 数位 DP
      • 记忆化搜索(同时跑上下界)
    • 倍增优化 DP
    • 斜率优化 DP(CHT)
    • WQS 二分优化 DP(凸优化 DP / 带权二分)
    • 树形 DP
      • 树的直径个数
      • 在任一直径上的节点个数
      • 树上最大独立集
      • 树上最小顶点覆盖
      • 树上最小支配集
      • 树上最大匹配
      • 换根 DP(二次扫描法)
        • 简单写法
        • 维护最大次大写法
        • 前后缀分解写法(适用性最广)
  • 图论 graph.go
    • 链式前向星
    • DFS 常用技巧
    • BFS 常用技巧
    • 欧拉回路和欧拉路径
      • 无向图
      • 有向图
      • 完全图
    • 割点
    • 割边(桥)
    • 双连通分量(BCC)
      • v-BCC
      • e-BCC
    • 仙人掌 & 圆方树
    • 最短路
      • Dijkstra
      • SPFA(队列优化的 Bellman-Ford)
        • 差分约束系统
      • Floyd-Warshall
      • Johnson
      • 0-1 BFS(双端队列 BFS)
      • 字典序最小最短路
      • 同余最短路
    • 最小环
    • 最小斯坦纳树
    • 最小生成树(MST)
      • Kruskal
      • Prim
    • 单度限制最小生成树
    • 次小生成树
    • 曼哈顿距离最小生成树
    • 最小差值生成树
    • 最小树形图
      • 朱刘算法
    • 二分图判定(染色)
    • 二分图找奇环
    • 二分图最大匹配
      • 匈牙利算法
    • 带权二分图最大完美匹配
      • Kuhn–Munkres 算法
    • 拓扑排序
    • 强连通分量(SCC)
      • Kosaraju
      • Tarjan
    • 2-SAT
    • 基环树
    • 最大流
      • Dinic
      • ISAP
      • HLPP
    • 最小费用最大流
      • SPFA
      • Dijkstra
    • 三元环计数
    • 四元环计数
    • 树上问题 graph_tree.go
      • 直径
      • 重心
      • 点分治
      • 点分树
      • 最近公共祖先(LCA)
        • 倍增
        • ST 表
        • Tarjan
        • 树上差分
        • 虚树
      • 重链剖分(HLD)
      • 长链剖分
      • 树上启发式合并(small to large)
        • 按大小合并
        • 轻重儿子合并
      • 树分块
      • Prufer 序列
  • 其他
    • 位运算笔记 bits.go
      • bitset
      • 区间位运算 trick(含 GCD)
    • 二分 三分 sort.go
      • 二分答案
      • 0-1 分数规划
      • 整体二分
    • 搜索 search.go
      • 枚举排列
      • 枚举组合
      • 生成下一个排列
      • 康托展开
      • 逆康托展开
      • 枚举子集
        • Gosper's Hack
      • 折半枚举(Meet in the middle)
        • 超大背包问题
    • 随机算法 rand.go
      • 模拟退火
    • 基础算法 common.go
      • 算法思路整理
      • 分组循环
      • 滑动窗口
      • 前缀和
      • 同余前缀和
      • 二维前缀和
      • 菱形区域和
      • 斜向前缀和
        • 菱形边界和
      • 等腰直角三角形区域和
        • 金字塔区域和
      • 二阶差分
      • 二维差分
      • 菱形二维差分
      • 离散化
    • 杂项 misc.go
  • 快速输入输出模板 io.go

分类题单

如何科学刷题?

  1. 滑动窗口与双指针(定长/不定长/单序列/双序列/三指针)
  2. 二分算法(二分答案/最小化最大值/最大化最小值/第K小)
  3. 单调栈(基础/矩形面积/贡献法/最小字典序)
  4. 网格图(DFS/BFS/综合应用)
  5. 位运算(基础/性质/拆位/试填/恒等式/思维)
  6. 图论算法(DFS/BFS/拓扑排序/最短路/最小生成树/二分图/基环树/欧拉路径)
  7. 🔥动态规划(入门/背包/状态机/划分/区间/状压/数位/数据结构优化/树形/博弈/概率期望)
  8. 常用数据结构(前缀和/差分/栈/队列/堆/字典树/并查集/树状数组/线段树)
  9. 数学算法(数论/组合/概率期望/博弈/计算几何/随机算法)
  10. 贪心与思维(基本贪心策略/反悔/区间/字典序/数学/思维/脑筋急转弯/构造)
  11. 链表、二叉树与一般树(前后指针/快慢指针/DFS/BFS/直径/LCA)

欢迎关注 B站@灵茶山艾府

如何选择题目 How to Choose Problems

Rating < 2100

这一阶段主要目标是提高对问题的观察能力。做构造题可以针对性地训练这一点。

选择难度在自己 rating 到 rating+200 范围内的构造题 (tag: constructive algorithms),按照过题人数降序做题,比如 [1700,1900] 区间的就是下面这个链接:

https://codeforces.com/problemset?order=BY_SOLVED_DESC&tags=constructive+algorithms%2C1700-1900

通过大量的构造题训练,提高观察能力,快速找到切题入口。具体见我在知乎上的这篇 回答。

Rating >= 2100(个人训练用,仅供参考)

见识更高的山、更广的海。

按人数从高到低,做 2200+ 的题目。建议不设置难度上限!由于按人数排序,难度分不会太高,不设上限可以避免错过高分好题。

我的 Codeforces 账号

0x3F

测试及对拍 Testing

编写一个 run(io.Reader, io.Writer) 函数来处理输入输出。这样写的理由是:

  • 在 main 中调用 run(os.Stdin, os.Stdout) 来执行代码;
  • 测试时,将测试数据转换成 strings.Reader 当作输入,并用一个 strings.Builder 来接收输出,将这二者传入 run 中,然后就能比较输出与答案了;
  • 对拍时需要实现一个暴力算法 runAC,参数和 run 一样。通过 随机数据生成器 来生成数据,分别传入 runAC 和 run,通过比对各自的输出,来检查 run 中的问题。

具体可以见 Codeforces 代码仓库 main,所有非交互题的代码及其对应测试全部按照上述框架实现。

例如:1439C_test.go

交互题的写法要复杂一些,需要把涉及输入输出的地方抽象成接口,详见 interactive_problem。

学习资料及题目 Resources

注:由于入门经典上选了很多区域赛的题,一部分题目可以在 GYM 上找到,这样可以就可以用 Go 编程提交了。

算法竞赛入门经典(第二版)

算法竞赛入门经典训练指南

算法竞赛入门经典训练指南(升级版)

算法竞赛进阶指南

算法竞赛入门到进阶

《算法竞赛》配套题单

国家集训队论文列表

算法竞赛 (ICPC, OI, etc) 论文,课件,文档,笔记等

算法竞赛课件分享 by hzwer

算法第四版 Java 源码

数据结构和算法动态可视化

OI Wiki

CP-Algorithms

The Ultimate Topic List (with Resources, Problems and Templates)

A Huge Update on The Ultimate Topic List

洛谷日报

All the good tutorials found for Competitive Programming

Codeforces Problem Topics

The Ultimate Topic List(with Tutorials, Problems, and Templates)

GeeksforGeeks 上的算法合集

Pepcy 模板

F0RE1GNERS 模板

https://github.com/hh2048/XCPC 含 jiangly 模板

https://www.cnblogs.com/alex-wei/p/contents.html

【模板整合计划】目录

算法学习笔记(目录)

洛谷模板题(建议按难度筛选)

能力全面提升综合题单

Luogu Problem List

洛谷原试炼场

Links of ICPC/CCPC Contests from China

AtCoder 题目分类

AtCoder 版《挑战程序设计竞赛》

AtCoder 版!蟻本 (初級編)

AtCoder 版!蟻本 (中級編)

AtCoder 版!蟻本 (上級編)

AtCoder 版!蟻本 (発展的トピック編)

待整理

【杂文】记一些有用的神奇网站

偶然在 GitHub 上发现的超长列表

算法竞赛训练中较难的部分

算法竞赛中可能不太会遇到的论文题

[杂谈]OI/ACM中冷门算法

Things I don't know

[meme] If you know at least 3 of these things and you are not red — you are doing it wrong. Stop learning useless algorithms, go and solve some problems, learn how to use binary search.

https://blog.csdn.net/calabash_boy/article/details/79973483

https://github.com/zimpha/algorithmic-library

https://www.luogu.com.cn/blog/command-block/blog-suo-yin-zhi-ding-post

https://wcysai.github.io/

https://www.luogu.com.cn/blog/Troverld/index

C++ @cache

其他 Others

My GoLand Live Templates and Postfix Completion settings

Useful Tools

GeoGebra

Draw Geometry

Draw Graph

OEIS

Wolfram|Alpha

ACD Ladders

Contests Filter

Codeforced

Codeforces Visualizer

Codeforces Solve Tracker

Another Codeforces Solve Tracker

AtCoder Problems

AtCoder Companions

AtCoder-Codeforces Rating converter

在线 Markdown + LaTeX

Rating and Difficulties

Open Codeforces Rating System

How to Interpret Contest Ratings

Codeforces: Problem Difficulties

Elo rating system

Stay Healthy

Exercises!