CSES Sorting and Searching 첫번째 문제입니다.
배열에서 중복되지 않는 숫자의 개수를 출력하는 문제입니다.
set을 사용해서 간단하게 구하였습니다.
Task
You are given a list of n integers, and your task is to calculate the number of distinct values in the list.
Input
The first input line has an integer n: the number of values.
The second line has n integers x1,x2,…,xn
Output
Print one integers: the number of distinct values.
Constraints
- 1≤n≤2⋅1e5
- 1≤xi≤1e9
Example
Input:
5
2 3 2 2 3
Output:
2
Code
#include<bits/stdc++.h>
using namespace std;
set<int> st;
int n, num;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin >> n;
while (n--){
cin >> num;
st.insert(num);
}
cout << st.size();
}
생각해야 하는 것
- 정렬한 후 for문 1회 돌면서 체크하는 방법도 있을 것 같습니다.
'알고리즘 > CSES' 카테고리의 다른 글
[C++] [CSES] Ferris Wheel (Sorting and Searching) (0) | 2023.02.14 |
---|---|
[C++] [CSES] Apartments (Sorting and Searching) (0) | 2023.02.13 |
[C++] [CSES] Grid Paths (Introductory Problems) (0) | 2023.02.10 |
[C++] [CSES] Digit Queries (Introductory Problems) (0) | 2023.02.10 |
[C++] [CSES] Chessboard and Queens (Introductory Problems) (0) | 2023.02.10 |