※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 24981번 문제인 Counting Liars이다.
문제는 아래 링크를 확인하자.

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

 

24981번: Counting Liars

Bessie the cow is hiding somewhere along the number line. Each of Farmer John's $N$ other cows ($1\le N\le 1000$) have a piece of information to share: the $i$-th cow either says that Bessie is hiding at some location less than or equal to $p_i$, or that B

www.acmicpc.net

L X꼴로 주어지는 범위는 [-INF,X]에 Bessie가 있다고 말한다는 뜻이고, G X꼴로 주어지는 범위는 [X, INF]에 Bessie가 있다고 말한다는 뜻이다.

 

거짓말을 하는 소가 가장 적은 케이스는, 위의 구간들이 최대한 많이 겹치는 곳을 기준으로 해당 위치를 포함하지 않는 구간의 개수를 세는 것이다.

 

이는 간단한 스위핑을 이용하여 계수할 수 있다.

 

아래는 제출한 소스코드이다.

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

pair<int, char> arr[1000];

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

	int mx = 0;
	int cnt = 0;
	int N; cin >> N;
	for (int i = 0; i < N; i++) {
		char c; int p; cin >> c >> p;
		arr[i] = make_pair(p, c);
		if (c == 'L') cnt++;
	}
	mx = cnt;

	sort(arr, arr + N);

	for (int i = 0; i < N; i++) {
		if (arr[i].second == 'L') cnt--;
		else cnt++;
		mx = max(mx, cnt);
	}

	cout << N - mx;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 24979 // C++] COW Operations  (0) 2022.04.28
[BOJ 24980 // C++] Photoshoot  (0) 2022.04.27
[BOJ 24982 // C++] Alchemy  (0) 2022.04.25
[BOJ 14919 // C++] 분포표 만들기  (0) 2022.04.24
[BOJ 14926 // C++] Not Equal  (0) 2022.04.24

+ Recent posts