200字
Luogu P1152 欢乐的跳
2025-10-01
2025-10-01

思路

  1. 计算相邻元素差值的绝对值:遍历数组,计算每两个相邻元素的差的绝对值

  2. 检查覆盖范围:使用一个布尔数组标记1到n-1范围内的数字是否出现

  3. 验证欢乐跳条件:检查1到n-1范围内的每个整数是否都出现在差值中

  4. 输出结果:如果所有数字都出现则输出"Jolly",否则输出"Not jolly"

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<long long> nums(n);
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }

    if (n == 1) {
        cout << "Jolly" << endl;
        return 0;
    }

    vector<bool> exists(n, false);
    for (int i = 1; i < n; i++) {
        long long diff = abs(nums[i] - nums[i-1]);
        if (diff >= 1 && diff <= n-1) {
            exists[diff] = true;
        }
    }

    bool isJolly = true;
    for (int i = 1; i < n; i++) {
        if (!exists[i]) {
            isJolly = false;
            break;
        }
    }

    cout << (isJolly ? "Jolly" : "Not jolly") << endl;

    return 0;
}

评论