B - Unique Bid Auction(暴力+STL)Codeforces Round #686 (Div. 3)
原题链接; http://codeforces.com/contest/1454/problem/B
测试样例
input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
output
-1
2
4
1
2
-1
题意: 给你一个长度为 n n n的数组,你需要在其中找到一个独一无二的数(即该数只出现过一次),若存在,则输出它的下标,若不存在,则输出 − 1 -1 −1。若存在多个独一无二的数,输出其中值最小的那个。
解题思路: 简单暴力即可。可以用两个map一个记录出现次数,一个记录出现位置。那么我们只需要遍历这个记录次数的map获取出现次数为 1 1 1的就好了。 记得每次都要清空map。
AC代码
/*
*blog:https://blog.csdn.net/hzf0701
*邮箱:unique_powerhouse@qq.com
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*/
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
using namespace std;
typedef long long ll;
const int maxn=2e5+5;//数组所开最大值
const int mod=1e9+7;//模
const int inf=0x3f3f3f3f;//无穷大
int t,n;
int a[maxn];
map<int,int> p1,p2;
void solve(){
int pos=inf;
for(auto p:p1){
if(p.second==1){
pos=p2[p.first];
break;
}
}
if(pos==inf)cout<<-1<<endl;
else cout<<pos<<endl;
}
int main(){
while(cin>>t){
while(t--){
cin>>n;
p1.clear();
p2.clear();
rep(i,1,n){
cin>>a[i];
p1[a[i]]++;
p2[a[i]]=i;
}
solve();
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)