1 solutions
-
0
题解:不完全柯西序列判定
这道题目虽然套用了“数学分析”和“柯西序列”的概念,但其本质是一个简单的数组差分序列性质判定问题。
1. 题目分析
定义提取:
- 距离序列:。
- 不完全柯西序列的条件:距离序列 必须是(不严格)递减的。
- 判定目标:给定一个长度为 的数组,判断它是否满足 ,都有 。
通俗理解: 就是看这个数组里,相邻两项之间的“跳跃幅度”是不是在不断缩小(或者保持不变)。如果幅度变大了,就不符合条件。
2. 解题步骤
- 计算差值的绝对值: 遍历数组,计算 。对于长度为 的数组,共有 个差值。
- 验证递减性:
检查是否满足 。
只要出现任何一个 ,立即判定为
No。 - 处理数据范围:
- :需要 的线性算法,不能用 。
- :差值的绝对值可能达到 ,在 C++ 中建议使用
long long以防万一,尽管int在 32 位环境下通常也够用。
3. 代码实现 (C++)
#include <iostream> #include <vector> #include <cmath> using namespace std; typedef long long ll; void solve() { int n; if (!(cin >> n)) return; vector<ll> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } // 计算并检查相邻项的距离是否递减 bool possible = true; ll last_dist = -1; for (int i = 0; i < n - 1; ++i) { // 计算当前距离 d_i = |a_i - a_{i+1}| ll current_dist = abs(a[i] - a[i + 1]); // 如果不是第一个距离,则与上一个距离比较 if (i > 0) { if (current_dist > last_dist) { possible = false; break; } } last_dist = current_dist; } if (possible) { cout << "Yes" << endl; } else { cout << "No" << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
4. 样例推演
样例 2:
1 100 50 75 75- (,OK)
- (,OK)
- (,OK)
- 结论:Yes。
样例 3:
1 100 300 300 300- (,违反递减性)
- 结论:No。
5. 复杂度分析
- 时间复杂度: 。只需对数组进行一次遍历,计算 次差值并做 次比较。
- 空间复杂度: 。用于存储输入数组 。如果边读入边处理,可以将空间复杂度优化到 。
Information
- ID
- 18
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 10
- Tags
- # Submissions
- 7
- Accepted
- 4
- Uploaded By