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.

Meaning of the questions: to a array of length n, the number of which can be deleted asked to form the rest of the arithmetic sequence number, if you simply delete a number of output index this number, otherwise output -1
ideas: Consider now the first number is deleted, or delete the second number, or delete a number from the back of the start of the third, the number to be deleted if the number is greater than 1, the current situation is not legal, if 3 case is not legal, the whole is not legitimate, output -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) {    /// If the number does not belong to the current arithmetic sequence, is recorded how many lower and recording mark 
12 is              CNT ++ ;
 13 is              ANS = MP [a [I]];
 14          }
 15          the else AI = D +;      /// AI = A1 + (. 1-I) * D, where the start is set to an a1, and then are added after each determination D 
16      }
 . 17      IF (CNT> . 1 ) ANS = - . 1 ;     /// If you want to delete more than one number, it is not legitimate 
18 is      return ANS;
 . 19  }
 20 is  int main () {
 21 is     :: sync_with_stdio iOS ( 0 );
 22 is      CIN >> n-;
 23 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 24          CIN >> A [I];
 25          MP [A [I]] = I;      // record the number of each index 
26 is      }
 27      IF (n-<= 3 ) {            /// If only the first three numbers put deleted, since the number of the remaining two must be formed arithmetic sequence 
28          the printf ( " . 1 \ n- " );
 29      }
 30      the else {
 31 is          Sort (A + . 1 , A + . 1n-+);             /// sort 
32          Q FD = (A [ 2 ], A [ . 3 ] -a [ 2 ], . 1 );      /// deleting the first 
33 is          QQ = FD (A [ . 1 ], A [ . 3 ] -a [ . 1 ], 2 );     /// deleting the second 
34 is          QQQ FD = (A [ . 1 ], A [ 2 ] -a [ . 1 ], . 3 );    /// deleted from behind a three start 
35          IF (Q = -! . 1 ) the printf ( " % D \ n- " , Q);
 36          the else IF (QQ = -! . 1 ) the printf ( " % D \ n- " , QQ);
 37 [          the else  IF (! QQQ = - . 1 ) the printf ( " % D \ n- " , QQQ);
 38 is          the else the printf ( " -1 \ n " );
 39      }
 40  }
 41 is  / * **
 42 is  given an array of length n, the number of Q which can delete the remaining number of arithmetic sequence formed, if only remove a number of outputs at this number standard, the output otherwise -1
 43 is  now considered the first number is deleted, or delete the second number, or delete a number from the back of the start of the third, the number to be deleted if the number is greater than 1, the current the situation is not legal, and if these three cases are illegal, the whole is not legal, the output of -1
 44 is  ** * /

 

Guess you like

Origin www.cnblogs.com/brainm/p/11301281.html