AT_abc378_c 题解

· · 题解

题目传送门

思路

定义一个 map 或者 unordered_map,设其为 m,定义 m_i 为数字 iA 中最后出现的下标。

遍历 1n

AC CODE

#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=2e5+10;
int a[N],b[N];
unordered_map<int,int>mp;
int main(){
    memset(b,-1,sizeof(b));
    int n=read();
    for(int i=1;i<=n;++i)
        a[i]=read();
    for(int i=1;i<=n;++i){
        if(mp.find(a[i])!=mp.end())
            b[i]=mp[a[i]];
        mp[a[i]]=i;
    }
    for(int i=1;i<=n;++i)
        printf("%d ",b[i]);
    printf("\n");
    return 0;
}