Codeforces 567A: Lineland Mail - Solution & Explanation (C++/Python)
Автор: Ajmain Fayek Diganta
Загружено: 2025-11-21
Просмотров: 271
In this video, we solve **Codeforces Problem 567A: Lineland Mail**.
This is a great problem to practice greedy logic and working with sorted arrays and coordinate geometry on a 1D axis.
*🧠 The Problem Logic:*
We are given $n$ cities on a number line (sorted in ascending order). For every city, we need to find:
1. *Minimum Cost (Nearest Neighbor):* Since the array is sorted, the nearest city to city $i$ must be either its left neighbor ($i-1$) or its right neighbor ($i+1$).
2. *Maximum Cost (Farthest Neighbor):* The farthest city from city $i$ is always one of the endpoints of the line: either the very first city ($0$) or the very last city ($n-1$).
We can calculate this for every city in just one loop\!
*🔗 Problem Link:*
[https://codeforces.com/problemset/problem/...](https://codeforces.com/problemset/problem/...)
-----
*📊 Complexity Analysis:*
*Time Complexity:* $O(N)$ - We iterate through the list of cities exactly once.
*Space Complexity:* $O(N)$ - To store the coordinates of the cities.
*👨💻 Code Snippet Logic:*
```cpp
// For Min: min(abs(x[i] - x[i-1]), abs(x[i] - x[i+1]))
// For Max: max(abs(x[i] - x[0]), abs(x[i] - x[n-1]))
```
Hi, I'm Ajmain Fayek Diganta, a CSE student at BUET. I upload tutorials on Competitive Programming, LeetCode, and Algorithms to help you crack coding interviews and contests.
*🔔 Subscribe for more CP solutions\!*
\#Codeforces \#LinelandMail \#CompetitiveProgramming \#BUET \#CSE \#Algorithms \#ProblemSolving \#CPlusPlus \#CodingTutorial \#567A
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: