77 lines
No EOL
3.1 KiB
Text
77 lines
No EOL
3.1 KiB
Text
---
|
|
id: amortized
|
|
title: "Amortized Analysis"
|
|
author: Darren Yao
|
|
prerequisites:
|
|
- Silver - Introduction to Sorting
|
|
- Silver - Stacks & Queues
|
|
description: "?"
|
|
---
|
|
|
|
## Two Pointers
|
|
|
|
Two pointers refers to iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in linear time.
|
|
|
|
- [CSES Playlist](https://cses.fi/problemset/task/1141)
|
|
|
|
### Tutorials
|
|
|
|
- Intro to USACO 14.1
|
|
- CPH 8.1 (Amortized Analysis)
|
|
|
|
## Sliding Window
|
|
|
|
Let's envision a sliding window (or constant size subarray) of size $K$ moving left to right along an array, $a$. For each position of the window, we want to compute some information.
|
|
|
|
Let's store a `std::set` of integers representing the integers inside the window. If the window currently spans the range $i \dots j$, we observe that moving the range forward to $i+1 \dots j+1$ only removes $a_i$ and adds $a_{j+1}$ to the window. We can support these two operations and query for the minimum/maximum in the set in $O(\log N)$.
|
|
|
|
To compute the sum in the range, instead of using a set, we can store a variable $s$ representing the sum. As we move the window forward, we update $s$ by performing the operations $s -= a_i$ and $s += a_{j+1}$.
|
|
|
|
### Min Queue
|
|
|
|
- [cp-algorithms: Min Stack + Queue](https://cp-algorithms.com/data_structures/stack_queue_modification.html)
|
|
- learn about the "min queue" that CPH describes.
|
|
|
|
### Queue w/ Two Stacks
|
|
|
|
<optional-content title="Application">
|
|
|
|
Used to remove a factor of $O(\log N)$ in [USACO Plat - Mowing Mischief](http://www.usaco.org/index.php?page=viewproblem2&cpid=926).
|
|
|
|
</optional-content>
|
|
|
|
### Further Reading
|
|
|
|
- [Medium](https://levelup.gitconnected.com/an-introduction-to-sliding-window-algorithms-5533c4fe1cc7)
|
|
- [G4G](https://www.geeksforgeeks.org/window-sliding-technique/)
|
|
|
|
## Problems
|
|
|
|
- USACO
|
|
- [Silver - Diamond Collector](http://usaco.org/index.php?page=viewproblem2&cpid=643)
|
|
- sort and then use 2P
|
|
- [Silver - Paired Up](http://usaco.org/index.php?page=viewproblem2&cpid=738)
|
|
- sort and then use 2P
|
|
- [Gold - Haybale Feast](http://usaco.org/index.php?page=viewproblem2&cpid=767)
|
|
- Direct application of sliding window.
|
|
- Just 2P with additional set.
|
|
- [Plat - Fort Moo](http://usaco.org/index.php?page=viewproblem2&cpid=600)
|
|
- CF
|
|
- [Books](https://codeforces.com/problemset/problem/279/B)
|
|
- [Cellular Network](http://codeforces.com/problemset/problem/702/C) [](48)
|
|
- [USB vs. PS/2](http://codeforces.com/problemset/problem/762/B) [](53)
|
|
- [K-Good Segment](http://codeforces.com/problemset/problem/616/D) [](53)
|
|
- [(Long Title)](http://codeforces.com/problemset/problem/814/C) [](54)
|
|
- [Jury Meeting](http://codeforces.com/problemset/problem/853/B) [](90)
|
|
|
|
<problems-list>
|
|
<problem name="CSES Playlist" link="https://cses.fi/problemset/task/1141" starred difficulty="Intro" />
|
|
<problem name="CSES Sum of Two Values" link="https://cses.fi/problemset/task/1640" difficulty="Easy" />
|
|
</problems-list>
|
|
|
|
<optional-content title="Better Memory with Two Passes">
|
|
|
|
- [Plat - Train Tracking](http://www.usaco.org/index.php?page=viewproblem2&cpid=841)
|
|
- Extremely difficult.
|
|
|
|
</optional-content> |