保存第一天价格,按端点两项和中间三项的整数平均值计算第二天价格。
OJ: shumeng
题目 ID: CSP201809A
难度:入门
标签:模拟数组
日期: 2026-07-31 16:21
形式化题目
给定长度为
其中
思路
端点与中间分开处理
端点 a / b 即可实现向下取整。
新旧状态分离
所有
代码
cpp
/**
* Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
* rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
* rainboy的学习导航网站: https://idx.roj.ac.cn
* create_at: 2026-07-31 16:21
* update_at: 2026-08-17 22:41
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int n;
int price[MAXN]; // 第一天各商店的菜价
int next_price[MAXN]; // 第二天各商店的菜价
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> price[i];
}
// 两端商店只有一个邻居,取两项的平均值。
next_price[1] = (price[1] + price[2]) / 2;
next_price[n] = (price[n - 1] + price[n]) / 2;
// 中间商店取自己与左右邻居三项的平均值。
for (int i = 2; i <= n - 1; i++) {
next_price[i] = (price[i - 1] + price[i] + price[i + 1]) / 3;
}
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << next_price[i];
}
cout << '\n';
return 0;
}复杂度
- 时间:每个商店只计算一次,
。 - 空间:保存两天的价格数组,
。
总结
同步更新题的核心是区分旧状态和新状态:先完整读入旧值,再据此推出全部新值。两个端点的参与项数不同,单独处理即可,整数除法天然满足去尾规则。