C++:: boj 14501 < 퇴사 >

jahlee·2023년 9월 15일
0
post-thumbnail

처음에는 dp의 방식으로 푸는 문제인가 싶었지만 문제 조건과 풀이방법을 생각해보니 dfs를 사용하면 간단하게 풀수있는 문제였다. 크게 어렵지않은 문제이다.
주의할점은 상담 기간이 퇴사하는 날을 넘어가면 안된다는 점이다.

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

int answer = 0, n;

void dfs(int day, int income, vector<pair<int,int>>& schedule) {
	answer = max(answer, income);
	for (int i=day; i<n; i++) {
		int next_day = i + schedule[i].first;
		if (next_day > n) continue;// 퇴사날짜 넘어가면 스킵
		dfs(next_day, income + schedule[i].second, schedule);
	}
}

int main() {
	cin >> n;
	vector<pair<int,int>> schedule(n);
	for (int i=0; i<n; i++) {
		cin >> schedule[i].first >> schedule[i].second;
	}
	dfs(0, 0, schedule);
	cout << answer << "\n";
}

0개의 댓글