代码改变世界

深入解析:算法竞赛备考冲刺必刷题(C++) | 洛谷 AT_abc419_c King‘s Summit

2025-09-14 15:41  tlnshuju  阅读(19)  评论(0)    收藏  举报

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AT_abc419_c [ABC419C] King's Summit - 洛谷

【题目描述】

There is a grid with 1 0 9 10^9 109 rows and 1 0 9 10^9 109 columns. Let ( i , j ) (i,j) (i,j) denote the square at the i i i-th row from the top and j j j-th column from the left.
有一个包含 1 0 9 10^9 109 行和 1 0 9 10^9 109 列的网格。用 ( i , j ) (i,j) (i,j) 表示位于从上往下第 i i i 行、从左往右第 j j j 列的方格。

There are N N N people on the grid. Initially, the i i i-th person is at square ( R i , C i ) (R_i,C_i) (Ri,Ci).
网格上有 N N N 个人。初始时,第 i i i 个人位于方格 ( R i , C i ) (R_i,C_i) (Ri,Ci)

The time starts at 0 0 0. Each person can do the following move at times 1 , 2 , 3 , 4 , … 1,2,3,4,… 1,2,3,4,.
时间从 0 0 0 开始。每个人可以在时刻 1 , 2 , 3 , 4 , … 1,2,3,4,… 1,2,3,4, 执行以下移动操作:

  • Stay at the current position, or move to an 8 8 8-adjacent square. It is forbidden to leave the grid. Formally, let square ( i , j ) (i,j) (i,j) be the current square, and move to one of the squares$ (i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)$ that exists. Assume that the move takes no time.
    停留在当前位置,或移动至 8 8 8 邻接方格。禁止离开网格。形式化地说,设当前方格为 ( i , j ) (i,j) (i,j),可移动至存在的以下方格之一: ( i − 1 , j − 1 ) 、 ( i − 1 , j ) 、 ( i − 1 , j + 1 ) 、 ( i , j − 1 ) 、 ( i , j ) 、 ( i , j + 1 ) 、 ( i + 1 , j − 1 ) 、 ( i + 1 , j ) 、 ( i + 1 , j + 1 ) (i−1,j−1)、(i−1,j)、(i−1,j+1)、(i,j−1)、(i,j)、(i,j+1)、(i+1,j−1)、(i+1,j)、(i+1,j+1) (i1,j1)(i1,j)(i1,j+1)(i,j1)(i,j)(i,j+1)(i+1,j1)(i+1,j)(i+1,j+1)。假设移动不消耗时间。

Find the minimum possible time when the N N N people are at the same square.
N N N 个人抵达同一方格的最小可能时间。

【输入】

The input is given from Standard Input in the following format:

N
R_1 C_1
R_2 C_2
.
.
.
R_N C_N

【输出】

Output the answer.

【输入样例】

3
2 3
5 1
8 1

【输出样例】

3

【算法标签】

《洛谷 AT_abc419_c King’s Summit》 #贪心#

【代码详解】

#include <bits/stdc++.h>
  using namespace std;
  int n;
  // 点的数量
  int max_x = -1e9, min_x = 1e9;
  // x坐标的最大值和最小值
  int max_y = -1e9, min_y = 1e9;
  // y坐标的最大值和最小值
  int main()
  {
  // 输入点的数量
  cin >> n;
  // 遍历所有点,更新坐标范围
  for (int i = 1; i <= n; i++)
  {
  int x, y;
  cin >> x >> y;
  // 更新x坐标范围
  max_x = max(max_x, x);
  min_x = min(min_x, x);
  // 更新y坐标范围
  max_y = max(max_y, y);
  min_y = min(min_y, y);
  }
  // 计算并输出结果
  // 计算x方向和y方向需要的最大半径,取两者中的较大值
  cout <<
  max((max_x - min_x + 1) / 2, (max_y - min_y + 1) / 2) << endl;
  return 0;
  }

【运行结果】

3
2 3
5 1
8 1
32