hades

[Baekjoon] 30804๋ฒˆ: ๊ณผ์ผ ํƒ•ํ›„๋ฃจ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 30804๋ฒˆ: ๊ณผ์ผ ํƒ•ํ›„๋ฃจ

hades1 2024. 7. 30. 20:57

๐Ÿฅ… ๋ฌธ์ œ

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

 

๐Ÿ” ์„ค๊ณ„

๊ผฌ์น˜์— ๊ณผ์ผ์„ ๋ชจ๋‘ ๊ฝ‚์•„๋†“๊ณ  ์–‘๋์—์„œ ๋นผ๋Š” ๊ฒƒ์€ ์•ž์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋ผ์šฐ๊ณ  ๊ฝ‚ํžŒ ๊ณผ์ผ์˜ ์ข…๋ฅ˜๊ฐ€ 2๊ฐœ๋ฅผ ๋„˜์œผ๋ฉด ์•ž์— ์žˆ๋Š” ๊ณผ์ผ์„ ๋นผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์•„์ง ๊ฝ‚ํžˆ์ง€ ์•Š์€ ๊ณผ์ผ๋“ค์€ ๋บ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๊ณผ์ผ์„ ํ•˜๋‚˜์”ฉ ๋ผ์šฐ๊ณ , ๊ผฌ์น˜์— ํ˜„์žฌ ์—†๋Š” ๊ณผ์ผ์ด๋ผ๋ฉด ๊ณผ์ผ์˜ ์ข…๋ฅ˜๋ฅผ ํ•˜๋‚˜ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ๊ผฌ์น˜์— ๊ฝ‚ํ˜€์žˆ๋Š” ๊ณผ์ผ์˜ ์ข…๋ฅ˜๊ฐ€ 2๊ฐœ๋ฅผ ์ดˆ๊ณผํ•œ๋‹ค๋ฉด, 2๊ฐœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์•ž์—์„œ๋ถ€ํ„ฐ ๋บ€๋‹ค. 2๊ฐœ๊ฐ€ ๋˜์—ˆ์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int n, fruit, type = 0, result = 0;
vector<int> fruit_count(10);
queue<int> stick;

int main(void)
{	
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	
	for (int i = 0; i < n; i++) {
		cin >> fruit;
		stick.push(fruit);

		if (fruit_count[fruit] == 0) {
			type += 1;
		}
		fruit_count[fruit] += 1;

		while (type > 2) {
			int front_fruit = stick.front();
			stick.pop();
			fruit_count[front_fruit] -= 1;
			if (fruit_count[front_fruit] == 0) {
				type -= 1;
			}
		}
		
		result = max(result,(int)stick.size());
	}
	cout << result << "\n";

	return 0;
}