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_...

C++ Day 33

  C++ Day 33: Smart Pointers & Memory Management 🔹 1. wherefore forward pointers in c++ hand-operated green / cancel is error-prone: memory leaks 🧠 double deletes ❌ dangling pointers 💥 smart pointers care store mechanically exploitation raii (Supply skill is initialization) 🔹 ii. Types of Smart Pointers in C++ ✅ std::unique_ptr Sole ownership of a Supply. Cannot be copied. Automatically deletes the Supply when it goes out of scope. cpp Copy Edit #include  unique_ptr ptr1 = make_unique(10); cout << *ptr1 << endl; // 10 You can transfer ownership: cpp Copy Edit unique_ptr ptr2 = move(ptr1); ✅ std::shared_ptr Shared ownership multiple shared_ptrs can point to the same object. Uses reference counting to track how many owners. cpp Copy Edit shared_ptr p1 = make_shared(100); shared_ptr p2 = p1;  // Reference count = 2 When count goes to 0 memory is released. ✅ std::weak_ptr Non-owning reference to a shared_ptr-managed object. Used to break cyclic references ...