Skip to main content

C++ Day 39

  C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...

C++ Day 36

 

Day 36: Binary Search on Answer (BSOA)

A high-level competitive programming technique.


🚀 1. What is Binary Search on Answer?

You use binary search on the value of the answer when:

1️⃣ The answer lies in a range (like 1 to 1e18)
2️⃣ For any candidate mid, you can check whether it's possible, valid, or feasible
3️⃣ If mid is valid → all values < mid or > mid are also valid (monotonic property)

This works for problems like:

  • minimum time

  • minimum capacity

  • maximum possible score

  • partitioning array into k parts

  • scheduling

  • optimization


🚦 2. Template

long long lo = L, hi = R, ans = R; while (lo <= hi) { long long mid = (lo + hi) / 2; if (check(mid)) { ans = mid; hi = mid - 1; // try to minimize } else { lo = mid + 1; } } cout << ans << "\n";

🧠 3. Example 1: Allocate Minimum Pages

Given n books, allocate to k students to minimize maximum pages.

check(mid) returns true if we can divide such that no one gets more than mid pages.


🧠 4. Example 2: Aggressive Cows

Place cows in stalls to maximize minimum distance.

Monotonic:
If distance d is possible → any smaller distance is possible.


🔥 5. Example 3: Minimum Maximum Subarray Sum

Split array into k parts so that maximum part sum is minimized.

Classic check(mid) → greedily cut segments.


6. Example Template Check Function

bool check(long long limit) { long long sum = 0; int parts = 1; for (long long x : arr) { if (x > limit) return false; if (sum + x > limit) { parts++; sum = x; } else { sum += x; } } return parts <= k; }

7. Tips

  • The range [L, R] is usually:

    • L = max element

    • R = sum of elements

  • Always check monotonicity

  • Prefer long long

  • Use mid = lo + (hi - lo)/2 if overflow is a concern


📚 8. Practice Problems

Try solving these to master BSOA:

  1. Aggressive Cows (SPOJ, classic)

  2. Book Allocation (GFG)

  3. Split Array Largest Sum (LeetCode Hard)

  4. Koko Eating Bananas

  5. Factory Machines (CSES)

  6. Apartments variant with maximize/minimize differences

  7. Minimize Max Distance to Gas Station


🎯 Summary

Binary Search on Answer =
➡️ Search answer range
➡️ Check mid validity
➡️ Use monotonic property
➡️ Powerful for optimization problems


If you want, I can give:

✔ Notes PDF
✔ 10 practice questions
✔ A hard BSOA problem with full explanation

Just tell me!

Comments

Popular posts from this blog

C++ Day 35

  C++ Day 34: Layout Layouts (Part 2) We’ll cover: Constructer Layout Adjuster Layout Decorator Layout practise Task 🔹 1. developer form (creational) used to make compound objects measure away step ✅ employ case: you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters example: cpp copy edit class calculator {     train Methodor gpu ram; public:     family developer {         train Methodor gpu ram;     public:         developer setcpu(string c) { Methodor = c; take *this; }         developer setgpu(string g) { gpu = g; take *this; }         developer setram(string r) { run = r; take *this; }         calculator Construct() {             take Calculater(cpu gpu ram);         }     };     Calculater(string snow train m train r) : cpu(c) gp...

C++ Day 39

  C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...

CSES Increasing Subsequence solution

 You are given an array containing  n n n integers. Your task is to determine the longest increasing subsequence in the array, i.e., the longest subsequence where every element is larger than the previous one. A subsequence is a sequence that can be derived from the array by deleting some elements without changing the order of the remaining elements. Input The first line contains an integer n n n : the size of the array. After this there are n n n integers x 1 , x 2 , … , x n x_1,x_2,\ldots,x_n x 1 ​ , x 2 ​ , … , x n ​ : the contents of the array. Output Print the length of the longest increasing subsequence. Constraints 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1 ≤ n ≤ 2 ⋅ 1 0 5 1 ≤ x i ≤ 1 0 9 1 \le x_i \le 10^9 1 ≤ x i ​ ≤ 1 0 9 Example Input: 8 7 3 5 3 6 2 9 8 Output: 4 #include < bits / stdc ++. h > using namespace std ; void solve (){ int n ; cin >> n ; vector <int> arr ( n ); for ( int i = 0 ; i < n ; i ++)...