PS/그리디 알고리즘[Greedy]

백준 1449 수리공 항승

SeungbeomKim 2022. 7. 1. 14:44
반응형

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

 

1449번: 수리공 항승

첫째 줄에 물이 새는 곳의 개수 N과 테이프의 길이 L이 주어진다. 둘째 줄에는 물이 새는 곳의 위치가 주어진다. N과 L은 1,000보다 작거나 같은 자연수이고, 물이 새는 곳의 위치는 1,000보다 작거나

www.acmicpc.net

<코드>

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
	int n,l;
	int cnt=1;
	cin>>n>>l;
	int *list = new int[n];
	for(int i=0;i<n;i++)
	{
		cin>>list[i];
	}
	sort(list,list+n);
	int start = list[0];
	for(int i=1;i<n;i++)
	{
		
		if(list[i]-start+1>l) //좌우 0.5 간격이기 때문에 +1을 해줌 
		{
			cnt++;
			start = list[i];
		}
	}
	cout<<cnt<<'\n';
}

<풀이과정>

i=0에서는 tape를 추가했다고 가정하고 start(시작점)을 list[0]으로 둔다.

i=1 부터는, list[i]-start(시작점)+1(좌우 0.5간격)>L(테이프의 길이)이면 tape의 개수 cnt를 1 증가시켜주면 끝이다. 

반응형

'PS > 그리디 알고리즘[Greedy]' 카테고리의 다른 글

백준 13164 행복 유치원(c++)  (1) 2022.07.11
백준 19941 햄버거 분배  (0) 2022.07.03
백준 18310 안테나  (0) 2022.06.30
백준 2812 크게 만들기  (0) 2022.06.30
백준 2012 등수매기기  (0) 2022.06.30