-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
There was a problem hiding this 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 μ μΌλ©΄ μ½λμ μ ννμ΄ λΌμ λ 보기 μ’μ κ² κ°μμ
if n == 1: | ||
return max(stickers[0][0], stickers[1][0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄ μΌμ΄μ€λ₯Ό μκ°λͺ»ν΄μ νΈλλ° μ’ μ€λκ±Έλ Έλ€μ...
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νμ΄μ¬μ 볡μ‘ν μ
λ ₯μ μ΄λ κ² λ°λκ΅°μ...!
μμ μ κΈ°ν©λλ€..
There was a problem hiding this 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;
}
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/9465
βοΈ μμλ μκ°
50min
β¨ μλ μ½λ
κ° μ΄μ μ΅μ μ μλ₯Ό ꡬνλ κ³Όμ μμ μ΄μ μ΄μ μ΅μ μ μ κ³μ°μ΄ λ°λ³΅λλ€λ μ¬μ€μ μΈμ§ν μμ λΆν° λμ κ³νλ²μ μ¬μ©ν΄μΌκ² λ€κ³ μκ°.
-> i-1κ³Ό i-2 μ΄μμ λ€λ₯Έ νμ κ°λ§μ μ νν΄λ μΆ©λΆν μ΅μ μ κ²°κ³Όλ₯Ό 보μ₯ν μ μμμ μ΄μ©.
첫 λ²μ§Έ νμ iλ²μ§Έ μ€ν°μ»€λ₯Ό μ ννλ κ²½μ°:
dp[0][i] = max(dp[1][i-1], dp[0][i-2], dp[1][i-2]) + stickers[0][i]
λ λ²μ§Έ νμ iλ²μ§Έ μ€ν°μ»€λ₯Ό μ ννλ κ²½μ°:
dp[1][i] = max(dp[0][i-1], dp[0][i-2], dp[1][i-2]) + stickers[1][i]