题解:P1152 欢乐的跳

· · 题解

首先计算相邻两个数的差的绝对值,然后将差值从小到大排序,如果第 i 个数不为 i,则不符合,否则符合。

注意:差值数组只有 n - 1 个数。

#include <bits/stdc++.h>
using namespace std;

int n;
int a[1005], c[1005];
int main() {
    cin>>n;
    for (int i = 1; i <= n; i++) cin>>a[i];
    for (int i = 1; i < n; i++) c[i] = abs(a[i] - a[i + 1]);
    sort(c + 1, c + n);
    for (int i = 1; i < n; i++) {
        if (c[i] != i) {
            cout<<"Not jolly";
            return 0;
        }
    }
    cout<<"Jolly";
    return 0;
}