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

 

이번에 볼 문제는 백준 6193번 문제인 Hungry Cows이다.
문제는 아래 링크를 확인하자.

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

 

6193번: Hungry Cows

Each of Farmer John's N (1 <= N <= 5,000) cows has a unique positive integer brand that fits nicely into 32 signed bits. He wishes the cows would line up in numerical order for feeding, but they never cooperate. To encourage good behavior, he allows a cow

www.acmicpc.net

이 문제는 주어지는 수열의 LIS(Longest Increasing Subsequence)의 길이를 구하는 문제이다. LIS 문제의 해법을 이용해 문제를 해결하자.

 

N의 크기가 작으므로 O(N2) 해법으로도 충분히 문제를 해결할 수 있다.

 

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

#include <iostream>
using namespace std;
typedef long long ll;

int N;
ll arr[5000];
int dp[5000];
int ans;

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

	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> arr[i];
		for (int j = 0; j < i; j++) {
			if (arr[j] < arr[i]) dp[i] = max(dp[i], dp[j]);
		}
		dp[i]++;

		ans = max(ans, dp[i]);
	}

	cout << ans;
}
728x90

+ Recent posts