Amusement Anticipation
amusement.c
,
amusement.cpp
,
amusement.c11
,
Amusement.java
,
amusement.py
It is was late Saturday morning at the end of October. The amus
ement park was going to be
open that afternoon for the first time after months of prolong
ed reconstruction.
Josse and Murry were sitting in the basement of the office build
ing at the park gate. They have
just finished debugging their advanced AI system management
of all fantastic attractions in the
park. “Work is done,” said Josse. “Now for some amusement. Ca
n you think of any algorithmic
problem?”
“Yes, of course,” replied Murry and grinned at his friend fro
m his heavily cluttered desk. “Con-
sider, for example, finite sequences of numbers. For me, the m
ost interesting sequences are those
that end with a long arithmetic sequence. In other words, I li
ke arithmetic sequences that span
from some index to the very last member. As there may be many su
ch subsequences, in order
to truly appreciate how interesting some sequence is, it is n
ecessary to determine which one is
the longest. Here is your initial sequence. You have to find th
e start of the longest continuous
arithmetic subsequence spanning to its end.”
“OK,” said Josse when he marked the correct place in the seque
nce. “That was easy. What
next?” “What next?” Repeated Murry and hesitated for a momen
t. Then he raised himself
determinedly from the chair. “Let’s go out to the park and find
some tougher problems there!”
Input Specification
There are more test cases. Each case consists of two lines. Th
e first line contains one integer
N
(1
≤
N
≤
1 000) specifying the length of the sequence. The second line
contains a sequence of
N
integers
X
i
(0
≤
X
i
≤
10
9
), separated by spaces.
Output Specification
For each test case, print a single line with the index of the fir
st member of the longest continuous
arithmetic subsequence that spans to the end of the given seq
uence. The index of the first element
in the sequence is always 1.
Sample Input
5
1 2 3 4 5
7
1 2 3 4 5 8 8
3
7 5 2
Output for Sample Input
1
6
2
AC代码(题意:找到间隔相同的最长的子串,这个串以最后一个元素为开头)
Select Code
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
//freopen("amusement.cpp", "r", stdin);
long long a[1000+10], ans;
int n, i;
while(scanf("%d",&n)!=EOF)
{
int f = 1;
ans = 0;
for(i = 1; i<=n; i++)
scanf("%lld",&a[i]);
ans = a[n]-a[n-1];
for(i = n; i>=1; i--)
{
if(a[i]-a[i-1]!=ans)
{
printf("%d\n",i);
f = 0;
break;
}
}
if(f==1)
printf("1\n");
}
return 0;
}