题解 CF1454B 【Unique Bid Auction】
没有用 STL
的吗?
我用的是自动排序的 set
与 当作桶的 unordered_map
,然后枚举一遍原来的数组,最终输出下标。
这里介绍了一个不太好但是很有用的东西:goto
,是无条件跳转语句,虽然功能强大,但有点……反正用得比较少,通常用 bool
判断是否执行接下来的而不是用 goto
直接调到语句后面。
其使用方法:
goto S;
cout << "not AKIOI\n";
S:
cout << "AKIOI\n";
输出: AKIOI
代码:
#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
unordered_map<int, int> mp;
int a[200005];
int main()
{
ios::sync_with_stdio(false);
register int T;
cin >> T;
while(T--)
{
mp.clear();
set<int> st;
int n;
cin >> n;
for(register int i = 1; i <= n; i++)
{
cin >> a[i];
mp[a[i]]++;
}
unordered_map<int, int>::iterator last = mp.end();
for(register unordered_map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if(it -> second == 1) st.insert(it -> first);
}
int value = *(st.begin());
for(register int i = 1; i <= n; i++)
{
if(a[i] == value) {cout << i << endl; goto G;}
}
cout << "-1\n";
G:{} //这里一定要加花括号,不然会CE
}
return 0;
}
这里使用到了一个 register
,其意思为寄存器,这种类型修饰的变量访问更快,但已经在 c++17
中移除了……
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现