今日已更新 80 条资讯 | 累计 20052 条内容
关于我们

标签:#competativeprogramming

找到 2 篇相关文章

AI 资讯

Competitive Programming Series — Session 2: Recursion and Backtracking

After covering the foundational building blocks in Session 1, the next step is one of the most important problem-solving techniques in all of programming: recursion . And once recursion feels comfortable, it unlocks a powerful search strategy called backtracking . These two concepts appear everywhere in competitive programming — Fibonacci, binary search, tree traversal, merge sort, dynamic programming, N-Queens, and more. They deserve their own spotlight. 🌟 What Is Recursion? A function is recursive if it calls itself. Instead of solving a problem in one go, a recursive function breaks it into a smaller version of the same problem, solves that, and repeats — until the problem becomes simple enough to answer directly. Three things define every recursive solution: The problem is expressed in terms of a smaller instance of itself Each call reduces the problem size There is a point where the problem becomes trivial and no further calls are needed — this is the base case The Nested Box Analogy Think of recursion like opening nested boxes. A big box contains a smaller box, which contains another, and so on. Eventually you find the item you were looking for. That innermost box is the base case. Without it, you would keep opening boxes forever — which is how you get a stack overflow, not a solution. Base Case and Recursive Case Every recursive function has exactly two parts: Recursive case — the problem is reduced in size and the function calls itself again. Base case — the terminating condition. No further recursive call is made. The function returns a direct answer. Both are non-negotiable. A function without a base case will keep calling itself, consuming stack memory until the program crashes. Example: Factorial 5! = 5 × 4! 4! = 4 × 3! 3! = 3 × 2! 2! = 2 × 1! 1! = 1 ← base case Each step reduces the problem by one. When the function hits 1! = 1 , it stops, and the results unwind back up the call stack. In pseudocode: function factorial(n): if n == 1: return 1 # base cas

2026-06-13 原文 →
AI 资讯

Competitive Programming Series — Session 1: The Foundations You Need Before Solving Problems

Competitive programming often looks like a race to write code as fast as possible. But the real secret is simpler: the best competitive programmers are not just faster typists — they are better at choosing the right data structure, the right algorithm, and the right complexity level for the job. Before we jump into recursion, dynamic programming, graphs, or those problems that make your brain do backflips, we need a solid base. This first session is exactly that. Let's begin. 🚀 1. Data Types: What Kind of Data Are You Storing? A data type tells a programming language what kind of value a variable holds and what operations are valid on it. Primitive Data Types The basic building blocks provided by the language itself: Integer — whole numbers: 5 , 100 , -3 Float / Double — decimal values: 3.14 , 99.5 Character — a single symbol: 'A' , 'z' Boolean — true or false User-Defined Data Types When primitive types are not enough, programmers define their own: Structs — group related fields under one name Classes — structs with behaviour (methods) attached Enums — a fixed set of named constants Typedefs / Aliases — rename existing types for clarity A Real-World Example Imagine building a food delivery app: An integer stores the number of items in the cart A float stores the total bill amount A boolean tracks whether the order has been delivered A class represents an entire Order — customer name, address, items, payment status Data types are essentially the labels on your containers. Without them, chaos begins early. 2. Data Structures: How Do You Organise Data? If data types answer what a value is, data structures answer how to organise many values efficiently. This is where competitive programming starts to get interesting. Linear Data Structures Elements arranged one after another, like people queuing at a ticket counter: Arrays — fixed-size, indexed, fast random access Linked Lists — dynamic size, efficient insertions and deletions Stacks — last in, first out (LIFO) Queues

2026-06-13 原文 →