Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17815 | Accepted: 5383 |
Description
A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.
Input
Each line of input contains an integer n < 3000 followed by n integers representing the sequence.
Output
For each line of input, generate a line of output saying "Jolly" or "Not jolly".
Sample Input
4 1 4 2 3 5 1 4 2 -1 6
Sample Output
Jolly Not jolly
Source
问题链接:POJ2575 ZOJ1879 UVA10038 Jolly Jumpers
问题简述:
每行输入n(n<=300)和n个整数,如果相邻的两个数之差正好是1到n-1之间的所有数,则输出“Jolly”,否则输出“Not jolly”。
问题分析: 需要标记数组diff[],标记已经出现的差值是否出现。其他都是套路。
程序说明:(略)
题记:(略)
参考链接:(略)
/* POJ2575 ZOJ1879 UVA10038 Jolly Jumpers */
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N = 3000;
int a[N];
bool diff[N];
bool check(int len)
{
memset(diff, false, sizeof(diff[0]) * len);
for(int i=1; i<len; i++) {
int d = abs(a[i] - a[i - 1]);
if(d > len - 1 || d == 0 || diff[d])
return false;
diff[d] = true;
}
return true;
}
int main()
{
int n;
while(~scanf("%d", &n)) {
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
printf("%s\n", check(n) ? "Jolly" : "Not jolly");
}
return 0;
}