Algorithm/Baekjoon
[Algorithm]Baekjoon 13144번 :: List of Unique Numbers
MaxLevel
2025. 3. 17. 00:24
https://www.acmicpc.net/problem/13144
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_set>
using namespace std;
long long n;
int arr[100001] = { 0 };
int check[100001] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> arr[i];
}
long long answer = 0;
check[arr[1]] = 1;
int start = 1;
int end = 1;
for (int i = 0; i < n; ++i) // 카운팅용. i 안씀. n번만큼만 ++start함.
{
while (end + 1 <= n && check[arr[end + 1]] == 0)
{
++end;
++check[arr[end]]; // 체크 (수열에 포함)
}
answer += end - start + 1;
--check[arr[start]]; // 수열에서 제외
++start;
}
cout << answer;
}
|
cs |
중복된 숫자가 없는 연속된수열들의 개수를 구하는 문제이다.
투포인터를 이용해야만 적절한 시간내에 해결할 수 있다.
친절한 설명은 아래글이 굿
https://codingnotes.tistory.com/182
[백준 13144번] List of Unique Numbers (C++)
https://www.acmicpc.net/problem/13144 13144번: List of Unique Numbers 길이가 N인 수열이 주어질 때, 수열에서 연속한 1개 이상의 수를 뽑았을 때 같은 수가 여러 번 등장하지 않는 경우의 수를 구하는 프로그램
codingnotes.tistory.com
start를 한단계씩 올리는거는 결국 최대 n번이기 때문에 for문으로 했다.