[BOJ/C++] 13144 List of Unique Numbers : Two Pointer

Hanbiยท2024๋…„ 7์›” 18์ผ
0

Problem Solving

๋ชฉ๋ก ๋ณด๊ธฐ
122/128
post-thumbnail
post-custom-banner

๋ฌธ์ œ

https://www.acmicpc.net/problem/13144

ํ’€์ด

๋ฌธ์ œ ๋ณด์ž๋งˆ์ž ์กฐํ•ฉ ๋ฌธ์ œ์ธ ์ค„ ์•Œ๊ณ , ์–ด๋–ป๊ฒŒ ํ’€์ดํ•ด์•ผ ํ•˜๋Š”์ง€ ๊ฐ์ด ์•ˆ์™”์—ˆ๋Š”๋ฐ ์ค‘๋ณต์ด ์—†๋Š” ์—ฐ์† ์ˆ˜์—ด์„ ์ฐพ๋Š” ํˆฌ ํฌ์ธํ„ฐ ๋ฌธ์ œ์˜€๋‹ค.

  • start ํฌ์ธํ„ฐ์™€ end ํฌ์ธํ„ฐ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ํƒ์ƒ‰

  • ์ˆ˜์—ด์ด 1 2 3 1 2์ธ ๊ฒฝ์šฐ, ๊ฐ๊ฐ ์ค‘๋ณต์ด ์—†๋Š” ๊ตฌ๊ฐ„์€
    1 2 3 1 2
    1 2 3 1 2
    1 2 3 1 2
    1 2 3 1 2
    1 2 3 1 2

  • ๊ฒฐ๊ณผ๊ฐ’์— ๊ฐ ๊ฒฝ์šฐ์— end-start๋ฅผ ๋”ํ•œ๋‹ค.
    start=0, end=3์ธ ๊ฒฝ์šฐ,
    1 1 2 1 2 3 ์„ธ ๊ฐ€์ง€์˜ ์ˆ˜์—ด์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ
    ๐Ÿ›‘์ฝ”๋“œ ๋กœ์ง์„ ๋ณด๋ฉด 1 2 3 ๊ตฌ๊ฐ„์˜ ๊ฒฝ์šฐ, end๋Š” 3๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๊ณ  break๋ฌธ์œผ๋กœ ๊ฐ

โš ๏ธstart๊ฐ€ 0 1 2 ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋ฉฐ for๋ฌธ ๋Œ ๋•Œ, end๊ฐ€ ๊ธฐ์กด ๋ฒ”์œ„๋ณด๋‹ค ์ž‘์•„์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†์œผ๋ฏ€๋กœ end๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์ง€๋Š” ์•Š์•„๋„ ๋จ

์ฝ”๋“œ

#include <iostream>

using namespace std;

int arr[100001];
bool visited[100001];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> arr[i];
	}

	int end = 0;
	long res = 0;

	for (int st = 0; st < N; st++) {
		while (end < N) {
			if (visited[arr[end]]) break;
			visited[arr[end]] = true;
			end++;
		}

		res += end - st;
		visited[arr[st]] = false;
	}

	cout << res;
}
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€