题解 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 中移除了……

浙公网安备 33010602011771号