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

D. Extra Element

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A sequence a1,a2,…,ak is called an arithmetic progression if for each i from 1 to k elements satisfy the condition ai=a1+c⋅(i−1) for some fixed c.

For example, these five sequences are arithmetic progressions: [5,7,9,11], [101], [101,100,99], [13,97] and [5,5,5,5,5]. And these four sequences aren’t arithmetic progressions: [3,1,2], [1,2,4,8], [1,−1,1,−1] and [1,2,3,3,3].

You are given a sequence of integers b1,b2,…,bn. Find any index j (1≤j≤n), such that if you delete bj from the sequence, you can reorder the remaining n−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≤n≤2⋅105) — length of the sequence b. The second line contains n integers b1,b2,…,bn (−109≤bi≤109) — elements of the sequence b.

Output

Print such index j (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.

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 4-th element, you can get the arithmetic progression [2,4,6,8].

Note to the second example. The original sequence is already arithmetic progression, so you can delete 1-st or last element and you will get an arithmetical progression again.

思路:

先排序,看前三个能不能组成一个等差数列,
1:不能组成:
就分三种情况:
(1):要去掉第一个数
(2):要去掉第二个数
(3):要去掉第三个数
2:能组成
看数组中有没有不能组成等差数列的数,如果只有一个就输出,有多个就输出-1,还有一种情况,就是全部是等差数列,那么就输出第一个或最后一个数的在原来的位置

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define dd double
#define ull unsigned long long
#define mes(x,y) memset(x,y,sizeof(x))
#define si(i) scanf("%d",&i)
#define ss(i) scanf("%s",(i))
#define sd(i) scanf("%lf",&i)
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

const int maxn=2e9+9;
const int mod=1e9+7;

ll GCD(ll a,ll b){//最大公约数
    return b==0?a:GCD(b,a%b);
}
struct node {
    ll x, y;
}a[200030];
bool cmp(node n1,node n2) {
    return n1.x < n2.x;
}
int main() {
    ll n, i, j, m;
    while (cin >> n) {
        for (i = 0; i < n; i++)cin >> a[i].x, a[i].y = i + 1;
        m = a[0].x;
        sort(a, a + n, cmp);
        // for (i = 0; i < n; i++)cout << a[i].x << " ";
        // cout << endl;
        if (n == 2) {
            cout << "1" << endl;
            continue;
        }
        ll d = a[1].x - a[0].x;
        if ((d + a[1].x) != a[2].x) {
            ll v1 = 1, v2 = 1, v3 = 1, q = 2;
            ll d1 = a[2].x - a[1].x, d2 = a[2].x - a[0].x;
            ll k1 = a[0].y, k2 = a[1].y, k3 = a[2].y;
            for (i = 3; i < n; i++) {
                if (a[i - 1].x + d1 != a[i].x)v1 = 0;
                if (a[i - 1].x + d2 != a[i].x)v2 = 0;
                if (a[i - q].x + d != a[i].x)v3 = 0;
                q = 1;
                if (v1 == 0 && v2 == 0 && v3 == 0)break;
            }
            if (v1 == 1) cout << k1 << endl;
            else if (v2 == 1)cout << k2 << endl;
            else if (v3 == 1)cout << k3 << endl;
            else cout << "-1" << endl;
        } else {
            ll d1 = d, d2 = d;
            ll k1 = a[0].y, k2 = a[0].y;
            if (a[3].x - d != a[2].x) {
                d2 = a[3].x - a[2].x;
                k2 = a[1].y;
            }
            ll v1 = 1, v2 = 1, flag = 1, jump1 = 1, total = a[0].x + d1 + d1+d1;
            for (i = 3; i < n; i++) {
                if (total != a[i].x) {
                    if (flag == 1) {
                        k1 = a[i].y;
                        flag = 0;
                    } else if (flag == 0) {
                        v1 = 0;
                    }
                } else {
                    total += d;
                }
                if (a[i - 1].x + d2 != a[i].x)v2 = 0;
                if (v1 == 0 && v2 == 0)break;
            }
            //cout<<v1<<" "<<v2<<endl;
            //cout << k1 << " " << k2 << endl;
            if (v1 == 1) cout << k1 << endl;
            else if (v2 == 1)cout << k2 << endl;
            else cout << "-1" << endl;
        }
    }
}







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GUESSERR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值