문제링크 : codeforces.com/contest/1454/problem/B
배열에 있는 값이 나온 횟수를 cnt배열에 저장했다.
cnt배열에 저장된 수 중 1번만 나온 수를 찾아서
다시 한번 반복문을 돌며 인덱스를 출력해주었다.
효율성이 애매할 것 같았는데 모든 테스트케이스에서의 sum of n이 20만 미만이라서 간신히 돌아간 듯 하다.
#include <iostream>
#include <algorithm>
#include <string>
#include <memory.h>
#include <vector>
#include <algorithm>
using namespace std;
int cnt[200001];
int arr[200001];
int main() {
int tc;
cin >> tc;
for (int i = 0; i < tc; i++) {
int n;
cin >> n;
int ans = 1000000;
memset(cnt, 0, sizeof(cnt));
for (int j = 1; j <= n; j++) {
cin >> arr[j];
cnt[arr[j]]++;
}
bool suc = false;
for (int j = 1; j <= 200000; j++) {
if (cnt[j] == 1) {
for (int k = 1; k <= n; k++) {
if (arr[k] == j) {
suc = true;
cout << k << '\n';
break;
}
}
if(suc)
break;
}
}
if (suc == false) {
cout << -1 << '\n';
}
}
}
'알고리즘 > 코드포스' 카테고리의 다른 글
[코드포스] edu_round #99(div2)-B JUMPS (0) | 2020.12.01 |
---|---|
[코드포스] Sum of Medians (0) | 2020.11.18 |