-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db56b28
commit 19cf55d
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 036 - Max Manhattan Distance (★5) | ||
|
||
## 解答 | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
#include <limits> // std::numeric_limits<> | ||
#include <algorithm> // std::min(), std::max() | ||
#include <cmath> // std::abs() | ||
|
||
struct Point | ||
{ | ||
long long x, y; | ||
}; | ||
|
||
int main() | ||
{ | ||
// N 個の点, Q 個のクエリ | ||
int N, Q; | ||
std::cin >> N >> Q; | ||
|
||
std::vector<Point> P(N); | ||
for (auto& p : P) | ||
{ | ||
long long X, Y; | ||
std::cin >> X >> Y; | ||
|
||
// 45 度の回転(√2 倍の拡大を伴う) | ||
p.x = (X - Y); | ||
p.y = (X + Y); | ||
} | ||
|
||
// 各軸における最小値と最大値を求める | ||
long long minX = std::numeric_limits<long long>::max(); | ||
long long maxX = std::numeric_limits<long long>::lowest(); | ||
long long minY = std::numeric_limits<long long>::max(); | ||
long long maxY = std::numeric_limits<long long>::lowest(); | ||
for (auto& p : P) | ||
{ | ||
minX = std::min(minX, p.x); | ||
maxX = std::max(maxX, p.x); | ||
minY = std::min(minY, p.y); | ||
maxY = std::max(maxY, p.y); | ||
} | ||
|
||
// 各クエリについて | ||
for (int i = 0; i < Q; ++i) | ||
{ | ||
int T; | ||
std::cin >> T; | ||
--T; | ||
|
||
// チェビシェフ距離の最大値を求める | ||
const Point p = P[T]; | ||
const long long a = std::abs(p.x - minX); | ||
const long long b = std::abs(p.x - maxX); | ||
const long long c = std::abs(p.y - minY); | ||
const long long d = std::abs(p.y - maxY); | ||
|
||
std::cout << std::max({ a, b, c, d }) << '\n'; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters