四大文化赛道完整展开
06-deliverables/appendix-code.md
红军补给均衡:相邻站点调拨统计 代码附录
站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。
文件类型Markdown
10-cases/s2-jh-06-supply-balance/06-deliverables/appendix-code.md
- 完整解题档案:complete-solution-dossier.md
Python 主实现
源文件:main.py
- 实现状态:当前已有可执行实现
import sys
def solve(data: str) -> str:
tokens = list(map(int, data.split()))
if not tokens:
return ""
it = iter(tokens)
n = next(it)
values = [next(it) for _ in range(n)]
total = sum(values)
if total % n != 0:
return "possible=NO"
target = total // n
prefix_diff = 0
moves = 0
units = 0
for index in range(n - 1):
prefix_diff += values[index] - target
if prefix_diff != 0:
moves += 1
units += abs(prefix_diff)
return "\n".join(
[
"possible=YES",
f"moves={moves}",
f"units={units}",
]
)
if __name__ == "__main__":
sys.stdout.write(solve(sys.stdin.read()).strip())
sys.stdout.write("\n")
C++ 对照实现
源文件:main.cpp
- 实现状态:当前已有可执行实现
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) {
return 0;
}
vector<long long> values(n);
long long total = 0;
for (int i = 0; i < n; ++i) {
cin >> values[i];
total += values[i];
}
if (total % n != 0) {
cout << "possible=NO\n";
return 0;
}
long long target = total / n;
long long prefix_diff = 0;
long long units = 0;
int moves = 0;
for (int i = 0; i < n - 1; ++i) {
prefix_diff += values[i] - target;
if (prefix_diff != 0) {
++moves;
units += llabs(prefix_diff);
}
}
cout << "possible=YES\n";
cout << "moves=" << moves << "\n";
cout << "units=" << units << "\n";
return 0;
}