Uva 10474 Where is the Marble?(模拟)
题目链接:https://vjudge.net/problem/UVA-10474
题意
将 n 个数从小到大排序,输出 q 次询问中每个数第一次出现的下标 (1-indexed) 。
思路
排序,映射。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, q, cnt = 1; while (cin >> n >> q and (n or q)) { int a[n]; for (int &i : a) cin >> i; sort(a, a + n); map<int, int> mp; for (int i = 0; i < n; i++) if (mp[a[i]] == 0) mp[a[i]] = i + 1; cout << "CASE# " << cnt++ << ":\n"; for (int i = 0; i < q; i++) { int v; cin >> v; if (mp[v]) cout << v << " found at " << mp[v] << "\n"; else cout << v << " not found" << "\n"; } } }