Skip to content
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

5-wnsmir #22

Merged
merged 1 commit into from
Nov 24, 2024
Merged

5-wnsmir #22

merged 1 commit into from
Nov 24, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 5, 2024

πŸ”— 문제 링크

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

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

50min

✨ μˆ˜λ„ μ½”λ“œ

각 μ—΄μ˜ 졜적 점수λ₯Ό κ΅¬ν•˜λŠ” κ³Όμ •μ—μ„œ 이전 μ—΄μ˜ 졜적 점수 계산이 λ°˜λ³΅λœλ‹€λŠ” 사싀을 μΈμ§€ν•œ μ‹œμ λΆ€ν„° λ™μ κ³„νšλ²•μ„ μ‚¬μš©ν•΄μ•Όκ² λ‹€κ³  생각.
-> i-1κ³Ό i-2 μ—΄μ—μ„œ λ‹€λ₯Έ ν–‰μ˜ κ°’λ§Œμ„ 선택해도 μΆ©λΆ„νžˆ 졜적의 κ²°κ³Όλ₯Ό 보μž₯ν•  수 μžˆμŒμ„ 이용.

  1. 첫 번째 ν–‰μ˜ i번째 μŠ€ν‹°μ»€λ₯Ό μ„ νƒν•˜λŠ” 경우:
    dp[0][i] = max(dp[1][i-1], dp[0][i-2], dp[1][i-2]) + stickers[0][i]

  2. 두 번째 ν–‰μ˜ i번째 μŠ€ν‹°μ»€λ₯Ό μ„ νƒν•˜λŠ” 경우:
    dp[1][i] = max(dp[0][i-1], dp[0][i-2], dp[1][i-2]) + stickers[1][i]

import sys
input = sys.stdin.read

def max_sticker_score(n, stickers):
    # n이 1일 λ•ŒλŠ” κ°„λ‹¨νžˆ 큰 값을 λ°˜ν™˜
    if n == 1:
        return max(stickers[0][0], stickers[1][0])
    
    # DP λ°°μ—΄ μ΄ˆκΈ°ν™”
    dp = [[0] * n for _ in range(2)]
    
    # μ΄ˆκΈ°κ°’ μ„€μ •
    dp[0][0] = stickers[0][0]
    dp[1][0] = stickers[1][0]
    
    # n이 2 이상인 경우 첫 두 열을 μ΄ˆκΈ°ν™”
    if n > 1:
        dp[0][1] = dp[1][0] + stickers[0][1]
        dp[1][1] = dp[0][0] + stickers[1][1]
    
    # DP λ°°μ—΄ μ±„μš°κΈ°
    for i in range(2, n):
        dp[0][i] = max(dp[1][i-1], dp[1][i-2]) + stickers[0][i] # i-2κΉŒμ§€ 처리 (점화식)
        dp[1][i] = max(dp[0][i-1], dp[0][i-2]) + stickers[1][i]
    
    # μ΅œλŒ“κ°’ λ°˜ν™˜
    return max(dp[0][n-1], dp[1][n-1])

# μž…λ ₯ 처리
data = input().strip().split()
T = int(data[0])  # ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ 수
index = 1
results = []

for _ in range(T):
    n = int(data[index])
    stickers = [
        list(map(int, data[index + 1:index + 1 + n])),
        list(map(int, data[index + 1 + n:index + 1 + 2 * n]))
    ]
    results.append(max_sticker_score(n, stickers))
    index += 2 * n + 1

# κ²°κ³Ό 좜λ ₯
for result in results:
    print(result)

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

λŒ€λ©΄ λ―ΈνŒ…λ•Œ μƒμ„Έν•˜κ²Œ μ„€λͺ…ν•΄μ£Όμ‹  λ¬Έμ œκ°€ λ‚˜μ™€μ„œ λ°˜κ°€μ› μŠ΅λ‹ˆλ‹€.

κ·Έλ•Œ μ„€λͺ… 듀은 κΈ°μ–΅λŒ€λ‘œ 잘 κ΅¬ν˜„ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

근데 λ§Œμ•½μ— μ„€λͺ…을 듣지 μ•Šμ•˜λ”λΌλ©΄ 1μ‹œκ°„ 이상 κ±Έλ¦¬λŠ” λ¬Έμ œμ˜€μ„ 것 κ°™λ„€μš”..
그리고 μ‹€μ „μ—μ„œ λ‚˜μ™”λ‹€λ©΄ μ•„λ§ˆ μ‹œκ°„μ•ˆμ— 풀지 λͺ»ν–ˆμ„ 것이라 μƒκ°λ˜λ„€μš”.
dp μ’€ 더 곡뢀 μ—΄μ‹¬νžˆν•΄μ•Ό ν•  것 κ°™λ„€μš”

쒋은 문제 κ°μ‚¬ν•©λ‹ˆλ‹€.

CPP CODE
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;

        vector<vector<int>> s(2, vector<int>(n));
        for (int i = 0; i < n; i++) 
            cin >> s[0][i];
        for (int i = 0; i < n; i++) 
            cin >> s[1][i];
        
        if (n == 1){
            cout << max(s[0][0],s[1][0]) << endl;
            continue;
        }

        vector<vector<int>> dp(2, vector<int>(n, 0));
        dp[0][0] = s[0][0];
        dp[1][0] = s[1][0];
        dp[0][1] = dp[1][0] + s[0][1];
        dp[1][1] = dp[0][0] + s[1][1];

        for (int i = 2; i < n; i++) {
            dp[0][i] = max(dp[1][i - 1], dp[1][i - 2]) + s[0][i];
            dp[1][i] = max(dp[0][i - 1], dp[0][i - 2]) + s[1][i];
        }
        cout << max(dp[0][n - 1], dp[1][n - 1]) << endl;
    }

    return 0;
}

그리고 μ½”λ“œ λ„£μœΌμ‹€λ•Œ
처음 λ°±ν‹± 3개 뒀에 python 적으면 μ½”λ“œμ— 색 ν‘œν˜„μ΄ λΌμ„œ 더 보기 쒋을 것 κ°™μ•„μš”

Comment on lines +6 to +7
if n == 1:
return max(stickers[0][0], stickers[1][0])
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 μΌ€μ΄μŠ€λ₯Ό 생각λͺ»ν•΄μ„œ ν‘ΈλŠ”λ° μ’€ μ˜€λž˜κ±Έλ Έλ„€μš”...

Comment on lines +30 to +42
data = input().strip().split()
T = int(data[0]) # ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ 수
index = 1
results = []

for _ in range(T):
n = int(data[index])
stickers = [
list(map(int, data[index + 1:index + 1 + n])),
list(map(int, data[index + 1 + n:index + 1 + 2 * n]))
]
results.append(max_sticker_score(n, stickers))
index += 2 * n + 1
Copy link
Collaborator

@kangrae-jo kangrae-jo Nov 11, 2024

Choose a reason for hiding this comment

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

νŒŒμ΄μ¬μ€ λ³΅μž‘ν•œ μž…λ ₯을 μ΄λ ‡κ²Œ λ°›λŠ”κ΅°μš”...!
μ™„μ „ μ‹ κΈ°ν•©λ‹ˆλ‹€..

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

μž¬λ°ŒλŠ” dp 문제 κ°μ‚¬ν•©λ‹ˆλ‹€!

my answer
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

int t;
int board[2][100001];
int dpCache[2][100001];
int max0 = INT_MIN;
int max1 = INT_MIN;

int dp(int n) {
	if (n == 1) {
		return max(board[0][0], board[1][0]);
	}

	dpCache[0][0] = board[0][0];
	dpCache[1][0] = board[1][0];

	dpCache[0][1] = board[0][1] + board[1][0];
	dpCache[1][1] = board[1][1] + board[0][0];

	for (int i = 2; i < n; i++) {
		dpCache[0][i] = board[0][i] + max(dpCache[1][i-1], dpCache[1][i-2]);
		dpCache[1][i] = board[1][i] + max(dpCache[0][i-1], dpCache[0][i-2]);
	}

	return max(dpCache[0][n-1], dpCache[1][n-1]);
}

int main() {
	cin >> t;
	for (int _ = 0; _ < t; _++) {
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> board[0][i];
		}
		for (int i = 0; i < n; i++) {
			cin >> board[1][i];
		}
		cout << dp(n) << '\n';

	}
	return 0;
}

@g0rnn g0rnn merged commit ba3f103 into main Nov 24, 2024
1 check passed
@g0rnn g0rnn deleted the 5-wnsmir branch November 24, 2024 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants