Simple problem. Just record the absolute values of the differences will do the job.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10038 C "Jolly Jumpers" */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #define MAX 3000
- int table[MAX];
- int jolly(int seq[], int n)
- {
- int i;
- bzero(table, MAX*sizeof(int));
- for (i = 0; i < n - 1; ++i) {
- int d = abs(seq[i] - seq[i+1]);
- if (d > n - 1 || table[d])
- return 0;
- else
- table[d] = 1;
- }
- return 1;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- char in[256];
- char out[256];
- strcpy(in, argv[0]);
- strcat(in, ".in");
- freopen(in, "r", stdin);
- strcpy(out, argv[0]);
- strcat(out, ".out");
- freopen(out, "w", stdout);
- #endif
- char *answer[] = {
- "Not jolly",
- "Jolly",
- };
- int i, n;
- int seq[MAX];
- while (scanf("%d", &n) != EOF) {
- for (i = 0; i < n; ++i)
- scanf("%d", &seq[i]);
- puts(answer[jolly(seq, i)]);
- }
- return 0;
- }