A sequence ?1,?2,…,??a1,a2,…,ak is called an arithmetic progression if for each ?i from 11 to ?k elements satisfy the condition ??=?1+?⋅(?−1)ai=a1+c⋅(i−1) for some fixed ?c.
For example, these five sequences are arithmetic progressions: [5,7,9,11][5,7,9,11], [101][101], [101,100,99][101,100,99], [13,97][13,97] and [5,5,5,5,5][5,5,5,5,5]. And these four sequences aren't arithmetic progressions: [3,1,2][3,1,2], [1,2,4,8][1,2,4,8], [1,−1,1,−1][1,−1,1,−1] and [1,2,3,3,3][1,2,3,3,3].
You are given a sequence of integers ?1,?2,…,??b1,b2,…,bn. Find any index ?j (1≤?≤?1≤j≤n), such that if you delete ??bj from the sequence, you can reorder the remaining ?−1n−1 elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.
Input
The first line of the input contains one integer ?n (2≤?≤2⋅1052≤n≤2⋅105) — length of the sequence ?b. The second line contains ?n integers ?1,?2,…,??b1,b2,…,bn (−109≤??≤109−109≤bi≤109) — elements of the sequence ?b.
Output
Print such index ?j (1≤?≤?1≤j≤n), so that if you delete the ?j-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.
思路: 先排序,再相邻做差,记录差值。出现的次数最多的差值即为等差公项loc。再遍历差值的数组,确定要删的元素坐标
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<map>
#include<cstring>
#define ll long long
using namespace std;
ll n;
map<ll ,ll>mp;
struct node{
ll nn;
ll i;
};
node a[1008611];
ll d[1008611],num[1008611],k=0;
bool cmp(node a,node b){
if(a.nn==b.nn)
return a.i<b.i;
return a.nn<b.nn;
}
int main(){
cin>>n;
mp.clear();
for(int i=0;i<n;i++){
cin>>a[i].nn;
a[i].i=i;
}
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++){
d[i]=a[i+1].nn-a[i].nn;
if(mp[d[i]]==0){
num[k++]=d[i];
}
mp[d[i]]++;
}
ll loc=0,maxn=0;
for(int i=0;i<k;i++){
if(mp[num[i]]>=maxn){
if(mp[num[i]]==maxn){
if(num[i]>loc)
loc=num[i];
}
else{
maxn=mp[num[i]];
loc=num[i];
}
}
}
ll ans=0,flag=0,cnt=0;
for(int i=0;i<n-1;i++){
if(d[i]!=loc){
if(i==n-2)ans=n-1;
else if(i==0&&d[i+1]==loc){
ans=0;
}
else{
if(d[i]+d[i+1]==loc){
ans=i+1;
i++;
}
else{
flag=1;
break;
}
}
cnt++;
}
}
if(cnt==1&&!flag){
cout<<a[ans].i+1<<endl;
}
else if(cnt==0&&!flag){
cout<<a[0].i+1<<endl;
}
else{
cout<<-1<<endl;
}
return 0;
}