-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add introduction of the algorithms for A B C D G H
- Loading branch information
Showing
6 changed files
with
745 additions
and
17 deletions.
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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"cells":[{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["5.000000\n"]}],"source":["import math\n","\n","# 定义函数,计算最小值\n","def calculate_min_value(a, b):\n"," # 根据给出的表达式 x = (sqrt(a) * b) / (sqrt(a) + 1)\n"," x = (math.sqrt(a) * b) / (math.sqrt(a) + 1)\n"," \n"," # 根据公式 f(x) = sqrt(x^2 + a) 和 g(x) = sqrt((b - x)^2 + 1)\n"," f_x = math.sqrt(x**2 + a)\n"," g_x = math.sqrt((b - x)**2 + 1)\n"," \n"," # 返回 f(x) + g(x)\n"," return f_x + g_x\n","\n","# 读取输入\n","a, b = map(float, input().split())\n","\n","# 计算并输出结果,保留小数点后 6 位\n","result = calculate_min_value(a, b)\n","print(f\"{result:.6f}\")"]},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["5.000000\n"]}],"source":["from math import sqrt\n","\n","# 定义函数,计算最小值\n","def calculate_min_value(a, b):\n"," ret = sqrt(b**2 + a + 2*sqrt(a) + 1)\n"," return ret\n","\n","# 读取输入\n","a, b = map(float, input().split())\n","\n","# 计算并输出结果,保留小数点后 6 位\n","result = calculate_min_value(a, b)\n","print(f\"{result:.6f}\")"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":[]}],"metadata":{"kernelspec":{"display_name":"base","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.5"}},"nbformat":4,"nbformat_minor":2} | ||
{"cells":[{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["5.000000\n"]}],"source":["import math\n","\n","# 定义函数,计算最小值\n","def calculate_min_value(a, b):\n"," # 根据给出的表达式 x = (sqrt(a) * b) / (sqrt(a) + 1)\n"," x = (math.sqrt(a) * b) / (math.sqrt(a) + 1)\n"," \n"," # 根据公式 f(x) = sqrt(x^2 + a) 和 g(x) = sqrt((b - x)^2 + 1)\n"," f_x = math.sqrt(x**2 + a)\n"," g_x = math.sqrt((b - x)**2 + 1)\n"," \n"," # 返回 f(x) + g(x)\n"," return f_x + g_x\n","\n","# 读取输入\n","a, b = map(float, input().split())\n","\n","# 计算并输出结果,保留小数点后 6 位\n","result = calculate_min_value(a, b)\n","print(f\"{result:.6f}\")"]},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["5.000000\n"]}],"source":["from math import sqrt\n","\n","# 定义函数,计算最小值\n","def calculate_min_value(a, b):\n"," ret = sqrt(b**2 + a + 2*sqrt(a) + 1)\n"," return ret\n","\n","# 读取输入\n","a, b = map(float, input().split())\n","\n","# 计算并输出结果,保留小数点后 6 位\n","result = calculate_min_value(a, b)\n","print(f\"{result:.6f}\")"]},{"cell_type":"markdown","metadata":{},"source":["将军饮马问题"]}],"metadata":{"kernelspec":{"display_name":"base","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.5"}},"nbformat":4,"nbformat_minor":2} |
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 |
---|---|---|
@@ -1 +1,352 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"6\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 输入处理\n", | ||
"n, k = map(int, input().split())\n", | ||
"citizens = list(map(int, input().split()))\n", | ||
"\n", | ||
"# 初始滑动窗口的和\n", | ||
"current_sum = sum(citizens[:k])\n", | ||
"max_sum = current_sum\n", | ||
"\n", | ||
"# 滑动窗口遍历\n", | ||
"for i in range(1, len(citizens) - k + 1):\n", | ||
" current_sum = current_sum - citizens[i - 1] + citizens[i + k - 1]\n", | ||
" max_sum = max(max_sum, current_sum)\n", | ||
"\n", | ||
"# 输出结果\n", | ||
"print(max_sum)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"5\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 输入处理\n", | ||
"n, k = map(int, input().split())\n", | ||
"\n", | ||
"# 构建上三角矩阵\n", | ||
"citizens = []\n", | ||
"for i in range(n - 1):\n", | ||
" citizens.append(list(map(int, input().split())))\n", | ||
"\n", | ||
"# 计算最大总和\n", | ||
"max_sum = 0\n", | ||
"\n", | ||
"# 遍历所有可能的起始路口\n", | ||
"for i in range(n - k):\n", | ||
" current_sum = 0\n", | ||
" for j in range(k):\n", | ||
" current_sum += citizens[i][j] # 累加相邻的市民数量\n", | ||
" \n", | ||
" max_sum = max(max_sum, current_sum)\n", | ||
"\n", | ||
"# 输出结果\n", | ||
"print(max_sum)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"6\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def max_citizens(n, k, citizens_matrix):\n", | ||
" # 将所有相邻路口之间的市民数量拉平成一个列表\n", | ||
" citizens = []\n", | ||
" for i in range(n - 1):\n", | ||
" citizens += citizens_matrix[i]\n", | ||
" \n", | ||
" # 使用滑动窗口,计算所有长度为 k 的连续子区间的和\n", | ||
" max_sum = 0\n", | ||
" current_sum = sum(citizens[:k])\n", | ||
" max_sum = current_sum\n", | ||
" \n", | ||
" for i in range(1, len(citizens) - k + 1):\n", | ||
" current_sum = current_sum - citizens[i - 1] + citizens[i + k - 1]\n", | ||
" max_sum = max(max_sum, current_sum)\n", | ||
" \n", | ||
" return max_sum\n", | ||
"\n", | ||
"# 读取输入\n", | ||
"n, k = map(int, input().split())\n", | ||
"citizens_matrix = []\n", | ||
"for i in range(n - 1):\n", | ||
" citizens_matrix.append(list(map(int, input().split())))\n", | ||
"\n", | ||
"# 计算并输出结果\n", | ||
"result = max_citizens(n, k, citizens_matrix)\n", | ||
"print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def max_citizens(n, k, C_input):\n", | ||
" # Initialize C[s][t] as a 2D list\n", | ||
" C = [[0] * (n + 1) for _ in range(n + 1)]\n", | ||
" idx = 0\n", | ||
" for s in range(1, n):\n", | ||
" for t in range(s + 1, n + 1):\n", | ||
" C[s][t] = C_input[idx]\n", | ||
" idx += 1\n", | ||
"\n", | ||
" # Compute pre_sum[s][x] = sum of C[s][x] to C[s][n]\n", | ||
" pre_sum = [[0] * (n + 2) for _ in range(n + 1)]\n", | ||
" for s in range(1, n):\n", | ||
" pre_sum[s][n] = C[s][n]\n", | ||
" for t in range(n - 1, s, -1):\n", | ||
" pre_sum[s][t] = C[s][t] + pre_sum[s][t + 1]\n", | ||
"\n", | ||
" max_total = 0\n", | ||
" # Iterate over all possible windows\n", | ||
" for l in range(1, n - k + 1):\n", | ||
" window_end = l + k - 1\n", | ||
" current_sum = 0\n", | ||
" for s in range(1, window_end + 1):\n", | ||
" current_sum += pre_sum[s][l + 1]\n", | ||
" if current_sum > max_total:\n", | ||
" max_total = current_sum\n", | ||
"\n", | ||
" return max_total\n", | ||
"\n", | ||
"# 读取输入\n", | ||
"import sys\n", | ||
"\n", | ||
"def main():\n", | ||
" import sys\n", | ||
" input = sys.stdin.read\n", | ||
" data = input().split()\n", | ||
" n = int(data[0])\n", | ||
" k = int(data[1])\n", | ||
" C_input = list(map(int, data[2:]))\n", | ||
" result = max_citizens(n, k, C_input)\n", | ||
" print(result)\n", | ||
"\n", | ||
"if __name__ == \"__main__\":\n", | ||
" main()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"14\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def max_citizens(n, k, C_input):\n", | ||
" # 初始化二维列表 C[s][t]\n", | ||
" C = [[0] * (n + 1) for _ in range(n + 1)]\n", | ||
" idx = 0\n", | ||
" for s in range(1, n):\n", | ||
" for t in range(s + 1, n + 1):\n", | ||
" C[s][t] = C_input[idx]\n", | ||
" idx += 1\n", | ||
"\n", | ||
" # 计算 pre_sum[s][t] = 从 t 到 n 的 C[s][t] 之和\n", | ||
" pre_sum = [[0] * (n + 2) for _ in range(n + 1)]\n", | ||
" for s in range(1, n):\n", | ||
" pre_sum[s][n] = C[s][n]\n", | ||
" for t in range(n - 1, s, -1):\n", | ||
" pre_sum[s][t] = C[s][t] + pre_sum[s][t + 1]\n", | ||
"\n", | ||
" max_total = 0\n", | ||
" # 遍历所有可能的窗口\n", | ||
" for l in range(1, n - k + 1):\n", | ||
" window_end = l + k - 1\n", | ||
" current_sum = 0\n", | ||
" for s in range(1, window_end + 1):\n", | ||
" current_sum += pre_sum[s][l + 1]\n", | ||
" if current_sum > max_total:\n", | ||
" max_total = current_sum\n", | ||
"\n", | ||
" return max_total\n", | ||
"\n", | ||
"def main():\n", | ||
" # 读取第一行:n 和 k\n", | ||
" first_line = input().strip().split()\n", | ||
" n = int(first_line[0])\n", | ||
" k = int(first_line[1])\n", | ||
"\n", | ||
" # 读取接下来的 n-1 行市民数量数据\n", | ||
" C_input = []\n", | ||
" for i in range(n - 1):\n", | ||
" line = input().strip()\n", | ||
" if line:\n", | ||
" nums = list(map(int, line.split()))\n", | ||
" C_input.extend(nums)\n", | ||
"\n", | ||
" # 计算结果并输出\n", | ||
" result = max_citizens(n, k, C_input)\n", | ||
" print(result)\n", | ||
"\n", | ||
"if __name__ == \"__main__\":\n", | ||
" main()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"当然,可以将程序调整为逐行从键盘读取输入。以下是调整后的完整 Python 代码,它逐行读取输入并计算受到宣传的最大市民数。\n", | ||
"\n", | ||
"### 问题回顾\n", | ||
"\n", | ||
"**目标**:在 **n** 个相邻路口中选择 **k** 个连续的路段安装宣传装置,以最大化受到宣传的市民总数。\n", | ||
"\n", | ||
"**输入格式**:\n", | ||
"1. 第一行:两个整数 `n` 和 `k`,表示路口数和可安装装置的数量。\n", | ||
"2. 接下来的 `n-1` 行:第 `i` 行包含 `n-i` 个整数,第 `j` 个数表示从路口 `i` 到路口 `i+j` 的每天通行市民数量。\n", | ||
"\n", | ||
"**输出格式**:\n", | ||
"- 输出一个整数,表示能够受到宣传的最大市民总数。\n", | ||
"\n", | ||
"### 解题思路\n", | ||
"\n", | ||
"1. **输入处理**:\n", | ||
" - 逐行读取输入,首先获取 `n` 和 `k`。\n", | ||
" - 接着读取 `n-1` 行的市民数量数据,并将其存储在一个一维列表中。\n", | ||
"\n", | ||
"2. **数据结构构建**:\n", | ||
" - 使用二维列表 `C[s][t]` 来表示从路口 `s` 到路口 `t` 的市民数量,其中 `s < t`。\n", | ||
"\n", | ||
"3. **前缀和计算**:\n", | ||
" - 为了高效计算每个窗口内的市民总数,预先计算每个起点 `s` 从 `t` 到 `n` 的前缀和 `pre_sum[s][t]`。\n", | ||
"\n", | ||
"4. **滑动窗口遍历**:\n", | ||
" - 遍历所有可能的 `k` 个连续路段窗口。\n", | ||
" - 对于每个窗口,计算所有经过这些路段的市民总数。\n", | ||
" - 更新并记录最大的市民总数。\n", | ||
"\n", | ||
"5. **输出结果**:\n", | ||
" - 输出计算得到的最大市民总数。\n", | ||
"\n", | ||
"### 代码说明\n", | ||
"\n", | ||
"1. **函数 `max_citizens`**:\n", | ||
" - **输入参数**:\n", | ||
" - `n`:路口数量。\n", | ||
" - `k`:可安装装置的数量。\n", | ||
" - `C_input`:包含所有路段市民数量的一维列表。\n", | ||
" - **功能**:\n", | ||
" - 将 `C_input` 转换为二维列表 `C[s][t]`。\n", | ||
" - 计算每个起点 `s` 从 `t` 到 `n` 的前缀和 `pre_sum[s][t]`。\n", | ||
" - 使用滑动窗口方法,遍历所有可能的 `k` 个连续路段,计算每个窗口内的市民总数,并记录最大值。\n", | ||
" - **返回值**:\n", | ||
" - 最大的市民总数。\n", | ||
"\n", | ||
"2. **函数 `main`**:\n", | ||
" - **功能**:\n", | ||
" - 逐行读取输入数据。\n", | ||
" - 调用 `max_citizens` 函数计算结果。\n", | ||
" - 输出结果。\n", | ||
"\n", | ||
"### 示例验证\n", | ||
"\n", | ||
"让我们验证一下提供的示例:\n", | ||
"\n", | ||
"**输入**:\n", | ||
"```\n", | ||
"4 1\n", | ||
"5 0 6\n", | ||
"5 3\n", | ||
"5\n", | ||
"```\n", | ||
"\n", | ||
"**输出**:\n", | ||
"```\n", | ||
"14\n", | ||
"```\n", | ||
"\n", | ||
"**解释**:\n", | ||
"- 当 `k=1` 时,选择安装在第 3-4 个路段,可以覆盖的市民数量为 `6 + 3 + 5 = 14`,这与输出一致。\n", | ||
"\n", | ||
"### 运行方式\n", | ||
"\n", | ||
"你可以将上述代码复制到你的 Python 环境中,运行后按照输入格式逐行输入数据。例如:\n", | ||
"\n", | ||
"```\n", | ||
"4 1\n", | ||
"5 0 6\n", | ||
"5 3\n", | ||
"5\n", | ||
"```\n", | ||
"\n", | ||
"然后程序会输出:\n", | ||
"\n", | ||
"```\n", | ||
"14\n", | ||
"```\n", | ||
"\n", | ||
"### 注意事项\n", | ||
"\n", | ||
"- **输入数据的格式**:确保每行输入的数据与题目描述相符,且没有多余的空格或空行。\n", | ||
"- **数据范围**:根据题目描述,`n` 的范围为 `2 ≤ n ≤ 500`,`k` 的范围为 `1 ≤ k ≤ n-1`,每对路口之间的市民数量不超过 `100`。\n", | ||
"\n", | ||
"这样调整后的代码能够正确地从键盘逐行读取输入,并计算出最大受宣传的市民总数。" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.