Skip to content

[동적 계획법] 10월 1일 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions BOJ_11053.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
#define MAX 1010
using namespace std;

int numElements; //배열 크기
int arr[MAX]; //배열
int lis[MAX]; //가장 긴 증가하는 부분 수열의 길이를 저장하는 배열

int getMax(int a, int b) { //두 수 중 더 큰 값을 반환하는 함수
return (a > b) ? a : b;
}
Comment on lines +9 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최대값을 반환하는 함수는 C++에 구현되어 있는 함수를 써주시면 좋을 것 같아요!


void calculateLIS() { //가장 긴 증가하는 부분 수열의 길이를 계산하는 함수
int maxLength = 0;

for (int i = 1; i <= numElements; i++) {
lis[i] = 1; //각 원소는 최소 길이 1의 수열을 가질 수 있음
for (int j = i - 1; j >= 1; j--) {
if (arr[i] > arr[j]) {
lis[i] = getMax(lis[i], lis[j] + 1);
}
}
maxLength = getMax(lis[i], maxLength);
}

cout << maxLength << endl; //길이 출력
}

int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

cin >> numElements;
for (int i = 1; i <= numElements; i++) {
cin >> arr[i];
}
calculateLIS(); //가장 긴 부분 수열 계산, 출력

return 0;
}
85 changes: 85 additions & 0 deletions BOJ_20923.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <deque>
using namespace std;

//게임을 진행하는 함수
void playGame(deque<int>& dodo, deque<int>& suyeon, int rounds, string& winner) {
deque<int> doGround; //도도의 카드
deque<int> suGround; //수연의 카드
Comment on lines +7 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deque 자료구조를 적절히 잘 사용해주셨네요!

bool isDodoTurn = true; //도도의 차례 여부
int cnt = 0; //게임 진행 횟수 카운트

while (true) {
cnt++; //게임 진행 횟수 증가

//차례에 따라 카드 내려놓기
if (isDodoTurn) {
doGround.push_back(dodo.back());
dodo.pop_back();
} else {
suGround.push_back(suyeon.back());
suyeon.pop_back();
}
Comment on lines +16 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비슷한 행위를 하는 코드가 반복적으로 쓰여져 있는 것을 확인할 수 있네요. 함수로 따로 뺴주시면 좋을 것 같아요!


//카드가 0개 됐는지 체크
if (dodo.empty()) {
winner = "su"; //도도의 카드가 0개면 수연이 승리
break;
} else if (suyeon.empty()) {
winner = "do"; //수연의 카드가 0개면 도도 승리
break;
}

//종 치는 상황 체크
if (!doGround.empty() && !suGround.empty() && doGround.back() + suGround.back() == 5) {
while (!doGround.empty()) {
suyeon.push_front(doGround.front());
doGround.pop_front();
}
while (!suGround.empty()) {
suyeon.push_front(suGround.front());
suGround.pop_front();
}
}
else if ((!doGround.empty() && doGround.back() == 5) || (!suGround.empty() && suGround.back() == 5)) {
while (!suGround.empty()) {
dodo.push_front(suGround.front());
suGround.pop_front();
}
while (!doGround.empty()) {
dodo.push_front(doGround.front());
doGround.pop_front();
}
}
Comment on lines +34 to +53

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비슷한 행위를 하는 코드가 반복적으로 쓰여져 있는 것을 확인할 수 있네요. 함수로 따로 뺴주시면 좋을 것 같아요!


//지정된 라운드 수가 되었을 때
if (cnt == rounds) {
int result = dodo.size() - suyeon.size();
if (result < 0) winner = "su";
else if (result > 0) winner = "do";
else winner = "dosu";
break;
}

//차례 변경
isDodoTurn = !isDodoTurn;
}
}

int main() {
int n, m; //n: 카드 개수, m: 게임 진행 라운드 수
cin >> n >> m;

deque<int> dodo(n); //도도의 카드 덱
deque<int> suyeon(n); //수연의 카드 덱

for (int i = 0; i < n; i++) {
cin >> dodo[i] >> suyeon[i];
}

string winner = ""; //승자
playGame(dodo, suyeon, m, winner);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수화 좋습니다!


cout << winner;
return 0;
}
40 changes: 40 additions & 0 deletions BOJ_2579.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<iostream>
#define MAX 301
using namespace std;

int numSteps; //계단의 수
int stepScores[MAX]; //각 계단의 점수
int maxScores[MAX]; //각 계단까지의 최대 점수를 저장하는 배열
Comment on lines +6 to +7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2

벡터 선언시에 n개 또는 n+1개만큼만 선언해준다면 메모리 측면에서 더욱 효율적인 코드가 될 것 같습니다 !


int getMax(int a, int b) { //두 수 중 더 큰 값을 반환하는 함수
return (a > b) ? a : b;
}


//최대 점수를 계산하는 함수
void calculateMaxScore() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수 따로 빼둔 거 너무 좋습니다 !

maxScores[1] = stepScores[1]; //첫 번째 계단의 최대 점수
maxScores[2] = stepScores[1] + stepScores[2]; //첫 두 계단의 최대 점수
maxScores[3] = getMax(stepScores[1] + stepScores[3], stepScores[2] + stepScores[3]); //세 번째 계단의 최대 점수

for (int i = 4; i <= numSteps; i++) {
maxScores[i] = getMax(maxScores[i - 2] + stepScores[i], maxScores[i - 3] + stepScores[i - 1] + stepScores[i]);
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

cin >> numSteps;
for (int i = 1; i <= numSteps; i++) {
cin >> stepScores[i];
}

calculateMaxScore(); //점수 계산

cout << maxScores[numSteps] << '\n';

return 0;
}