题目:http://codeforces.com/problemset/problem/1185/D
D. Extra Element
A sequence a1,a2,…,aka1,a2,…,ak is called an arithmetic progression if for each ii from 11 to kk elements satisfy the condition ai=a1+c⋅(i−1)ai=a1+c⋅(i−1) for some fixed cc.
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 b1,b2,…,bnb1,b2,…,bn. Find any index jj (1≤j≤n1≤j≤n), such that if you delete bjbj from the sequence, you can reorder the remaining n−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 nn (2≤n≤2⋅1052≤n≤2⋅105) — length of the sequence bb. The second line contains nn integers b1,b2,…,bnb1,b2,…,bn (−109≤bi≤109−109≤bi≤109) — elements of the sequence bb.
Output
Print such index jj (1≤j≤n1≤j≤n), so that if you delete the jj-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.
Examples
input
5 2 6 8 7 4
output
4
input
8 1 2 3 4 5 6 7 8
output
1
input
4 1 2 4 8
output
-1
Note
Note to the first example. If you delete the 44-th element, you can get the arithmetic progression [2,4,6,8][2,4,6,8].
Note to the second example. The original sequence is already arithmetic progression, so you can delete 11-st or last element and you will get an arithmetical progression again.
题意:给一个n个元素的数列,删除一个元素剩下的可不可以随意排列一个等差数列,如果可以,输出删除元素的序号,如果不能,输出-1.
解题思路:先把数组排序,然后算出相邻元素的差bi,统计差出现的次数,然后遍历数列,计算把当前元素删除数列会新增的差和删除的差,计算首项和尾项的差,求得公差,如果公差出现n-2次,则数组是等差数列。
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a<b?a:b
#define MAX(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))
#define MIN(a,b,c) (a<b?(a<c?a:c):(b<c?b:c))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
const int MAXN = 2750131;
ll T,n,m,i,j;
ll gcd(ll p,ll q)
{return q==0?p:gcd(q,p%q);}
struct point{
ll data,index;
}a[200005];
bool cmp(point x,point y){
return x.data<y.data;
}
int main()
{
map<ll,int>mp;
cin>>n;
ll sum=0;
ll b[200005];
for(i=0;i<n;i++){
cin>>a[i].data;
a[i].index=i;
}
if(n<4){
cout<<"1"<<endl;return 0;
}
sort(a,a+n,cmp);
for(i=1;i<n;i++){
ll s=a[i].data-a[i-1].data;
b[i]=s;
mp[s]++;
}
ll d,a1,an;
for(i=0;i<n;i++){
a1=a[0].data;
an=a[n-1].data;
if(i==0){
a1=a[1].data;
mp[b[1]]--;
}
else if(i==(n-1)){
an=a[n-2].data;
mp[b[n-1]]--;
}
else{
ll s1=b[i]+b[i+1];
mp[s1]++;
mp[b[i]]--;
mp[b[i+1]]--;
}
if((an-a1)%(n-2)){
}
else{
d=(an-a1)/(n-2);
if(mp[d]==(n-2)){
cout<<a[i].index+1<<endl;return 0;
}
}
if(i==0){
mp[b[1]]++;
}
else if(i==(n-1)){
mp[b[n-1]]++;
}
else{
ll s1=b[i]+b[i+1];
mp[s1]--;
mp[b[i]]++;
mp[b[i+1]]++;
}
}
cout<<"-1"<<endl;return 0;
}