Codeforces Round #568 (Div. 2) D. Extra Element

Extra Element
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence a1,a2,,aka1,a2,…,ak is called an arithmetic progression if for each ii from 11 to kkelements satisfy the condition ai=a1+c(i1)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 (1jn1≤j≤n), such that if you delete bjbj from the sequence, you can reorder the remaining n1n−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 (2n21052≤n≤2⋅105) — length of the sequence bb. The second line contains nn integers b1,b2,,bnb1,b2,…,bn (109bi109−109≤bi≤109) — elements of the sequence bb.

Output

Print such index jj (1jn1≤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
Copy
5
2 6 8 7 4
output
Copy
4
input
Copy
8
1 2 3 4 5 6 7 8
output
Copy
1
input
Copy
4
1 2 4 8
output
Copy
-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
思路:现在考虑是删第一个数,还是删第二个数,还是删从第三个开始的后面的一个数,如果要删的数的个数大于1,则当前的情况是不合法的,如果这3种情况都不合法,则整体是不合法的,输出-1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=2e5+5;
 5 int a[amn],n,q,qq,qqq;
 6 map<int,int> mp;
 7 int fd(int a1,int d,int det){
 8     int cnt=0,ans=1;
 9     ll ai=a1;
10     for(int i=1;i<=n;i++){
11         if(a[i]!=ai){   ///如果这个数不属于当前的等差数列,则记录有多少个并记录下标
12             cnt++;
13             ans=mp[a[i]];
14         }
15         else ai+=d;     ///ai=a1+(i-1)*d,这里是一开始an设为a1,然后每次判断后都加d
16     }
17     if(cnt>1)ans=-1;    ///如果要删的个数大于一个,则是不合法的
18     return ans;
19 }
20 int main(){
21     ios::sync_with_stdio(0);
22     cin>>n;
23     for(int i=1;i<=n;i++){
24         cin>>a[i];
25         mp[a[i]]=i;     ///记录下每个数的下标
26     }
27     if(n<=3){           ///如果只有3个数就把第一个删掉,因为剩下两个数必定可以形成等差数列
28         printf("1
");
29     }
30     else{
31         sort(a+1,a+1+n);            ///排序
32         q=fd(a[2],a[3]-a[2],1);     ///删去第一个
33         qq=fd(a[1],a[3]-a[1],2);    ///删去第二个
34         qqq=fd(a[1],a[2]-a[1],3);   ///删去从第三个开始的后面的一个
35         if(q!=-1)printf("%d
",q);
36         else if(qq!=-1)printf("%d
",qq);
37         else if(qqq!=-1)printf("%d
",qqq);
38         else printf("-1
");
39     }
40 }
41 /***
42 给一个长度为n的数组,问删掉哪个数能使剩下的数形成等差数列,若只需删除一个数就输出这个数的下标,否则就输出-1
43 现在考虑是删第一个数,还是删第二个数,还是删从第三个开始的后面的一个数,如果要删的数的个数大于1,则当前的情况是不合法的,如果这3种情况都不合法,则整体是不合法的,输出-1
44 ***/
原文地址:https://www.cnblogs.com/Railgun000/p/11301281.html