D. Extra Element

博客围绕等差数列展开,给出等差数列定义及示例。提出问题:给定整数序列,找到一个索引,删除该索引对应元素后,剩余元素重排可成等差数列。还给出解题思路,先排序、相邻做差记录差值,确定等差公项,再遍历差值数组确定删除元素坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值