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

문제 분석

모든 불이 꺼져있는 버튼 n개가 놓여져 있음.

입력된 것과 같은 상태로 최소의 버튼을 눌러서 만들어야 한다.

단, 버튼을 누르면 오른쪽 버튼 2개가 같이 눌림

함께 눌리는 것은 오른쪽 버튼이므로,

왼쪽부터 입력값과 비교하며 일치하지 않는 버튼을 누르면 됩니다.

 

소스 코드

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
using std::cin;
using std::cout;
int main()
{ 
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<bool> v(n);
	vector<bool> curr(n,false);
	int cnt = 0;

	for (int i = 0; i < n; i++)
	{
		bool b;
		cin >> b;
		v[i] = b;
	}

	for (int i = 0; i < n; i++)
	{
		if (v[i] != curr[i])
		{
			curr[i] = v[i];
			cnt++;
			for (int j = i+1; j <= i + 2; j++)
			{
				if (j< n && curr[j])
				{
					curr[j] = false;
				}
				else
				{
					curr[j] = true;
				}
			}
		}
	}
	cout << cnt;
}

'백준 > 실버' 카테고리의 다른 글

[백준 15688]- 수 정렬하기(c++)  (0) 2026.04.03
[백준 1622]- 공통 순열(c++)  (0) 2026.04.03
[백준 3060]- 욕심쟁이 돼지(c++)  (0) 2026.03.23
[백준 14916] - 거스름돈(c++)  (0) 2026.03.16
[백준 1340] - 연도 진행바  (0) 2026.03.05

+ Recent posts