-
368-B, *1100코드포스 2021. 7. 23. 16:24
Problem - 368B - Codeforces
codeforces.com
문제
- 배열 a=a1a2⋅⋅⋅an, (1≤n≤105)에 대해
- alal+1⋅⋅⋅an 중 서로 다른 수가 몇 개인지 m(1≤m≤105)번 출력
- O(nlog n) 정도의 알고리즘은 통과할 수 있다
O(log n!)
가장 큰 l부터 답을 구하여 배열 arr[l]에 기록한다
an을 set s 에 삽입한다. 그러면 O(log s.size)
ans[n]=s.size
an−1을 set s 에 삽입한다. 그러면 O(log s.size)
ans[n−1]=s.size
123456789101112131415161718192021222324252627282930#include <bits/stdc++.h>#define endl "\n"using namespace std;int arr[100001];int ans[100001];set<int> s;int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, m;cin >> n >> m;for(int i = 1; i <= n; i++) cin >> arr[i];for(int i = n; i >= 1; i--){s.insert(arr[i]);ans[i] = s.size();}int tmp;while(m--){cin >> tmp;cout << ans[tmp] << endl;}return 0;}cs'코드포스' 카테고리의 다른 글
1327-A, *1100 (0) 2021.07.24 1335-C, *1100 (0) 2021.07.23 313-B, *1100 (0) 2021.07.23 456-A, *1100 (0) 2021.07.23 519-B, *1100 (0) 2021.07.23