-
Notifications
You must be signed in to change notification settings - Fork 2
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
41-tgyuuAn #148
41-tgyuuAn #148
Conversation
μκ³ νμ
¨μ΅λλ€ μ΄λ€ μμΌλ‘ μλ€κ°λ€ νλμ§ κ³μ κ·Έλ¦¬κ³ μ§μ°κ³ λ°λ³΅νλ€κ° μκ²λμμ΅λλ€
T, W = map(int, input().split())
arr = [0] + [int(input()) for _ in range(T)]
dp = [[[0,0] for _ in range(W + 1)] for _ in range(T + 1)]
for t in range(1, T + 1):
dp[i][0][0] = dp[i - 1][0][0] + (arr[i] == 1)
for w in range(1, W + 1):
dp[t][w][0] = max(dp[t - 1][w][0], dp[t - 1][w - 1][1]) + (arr[t] == 1)
dp[t][w][1] = max(dp[t - 1][w][1], dp[t - 1][w - 1][0]) + (arr[t] == 2)
result = 0
for w in range(W + 1):
result = max(result, dp[T][w][0], dp[T][w][1])
print(result) μ°¨μμ μ€μΌ μ μμ§ μμκΉ μΆμ΄μ κ³ λ―Όν΄λ³΄λκΉ T, W = map(int, input().split())
arr = [int(input()) for _ in range(T)]
dp = [[0, 0] for _ in range(W + 1)]
for t in range(1, T + 1):
dp[0][0] = dp[0][0] + (arr[t - 1] == 2)
for w in range(1, W + 1):
dp[w][0] = max(dp[w][0], dp[w - 1][1]) + (arr[t - 1] == 1)
dp[w][1] = max(dp[w][1], dp[w - 1][0]) + (arr[t - 1] == 2)
result = 0
for w in dp:
result = max(result, max(w))
print(result) |
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.
μμ§ 3μ°¨μ λ°°μ΄μ μ΅μνμ§ μμμ μ½λ μ΄ν΄νλλ° μ‘°κΈ μκ°μ΄ κ±Έλ Έλ€μ
μλ§ μ½ν
λ³΄λ €λ©΄ μ΄ μ λλ νμ΄μΌνλκ΅°μ ...γ
γ
γ
μ½ν 보λλΌ μκ³ λ§μΌμ ¨μ΅λλ€ :)
T, W = map(int,input().split()) | ||
|
||
# DP[i][j][k] iμ΄μ [j]μμΉμ μκ³ [k]λ²μ νμλ₯Ό μ΄λνμ λ λ°μ μ μλ μλ μ | ||
DP = [[[0 for _ in range(W+1)] for _ in range(2+1)] for _ in range(T+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.
list μ΄κΈ°ν ν λ forλ¬Έμ μ¬μ©νλ λ²λ§ μμλλ°, νκ·λ μ½λλ³΄κ³ μ°Ύμ보λ '리μ€νΈ μ»΄ν리ν¨μ 'μ΄λΌλκ² μλκ΅°μ
μ½λ μ¬λ¬ μ€λ‘ μ΄κΈ°ν ν νμμμ΄ νμ€λ‘ κΉλνκ² 3μ°¨μ λ°°μ΄μ μ΄κΈ°ν ν κ²μ΄ μΈμμ μ΄μμ΅λλ€.
μ λ λ€μ list μ΄κΈ°νλλ κΌ ..! μ¬μ©ν΄λ³΄κ² μ΄λ€
DP = [[[0 for _ in range(W+1)] for _ in range(2+1)] for _ in range(T+1)] | ||
|
||
plums = [] | ||
for _ in range(T): |
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.
_μ μ¬μ©μ javaλ dartμμ private λ³μλͺ μ§μ λ μ¬μ©νλ κ²μΌλ‘λ§ λ΄€λλ°, pythonμμλ κ΄μ΅μ μΌλ‘ forλ¬Έ μμμ _λ₯Ό μ¬μ©νλ€κ³ νλλΌκ³ μ
μλ‘κ² κΉ¨λ«κ³ κ°λλ€
μλλ μ κ° μ΄ν΄ν _μ μλ―Έλ₯Ό μ 리ν΄λ³΄μμ΄μ
- μΈν°ν리ν°μμ λ§μ§λ§ κ°μ μ μ₯ν λ
- λ³μλ ν¨μλͺ μμ -> νΉλ³ν μλ―Έ λΆμ¬(ex. private λλ λ€λ₯Έ classμμ μ¬μ©νμ§ λͺ»ν λ)
- μ«μ μ리μ κ΅¬λΆ ex. 1_000_000_000
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.
μ¬λ΄μΌλ‘ λͺ¨λ λ΄λΆμμ _μ λΆμ¬μ ν¨μλ₯Ό μ μΈνλ€ ν΄λ κ°μ Έλ€ μΈ μ μκΈ΄ν©λλ€.
μκ³ λ¦¬μ¦ λ¬Έμ ν λ μμ£Ό μ°λ heapq λͺ¨λμ μ΅μνλ§ κ΅¬νλμ΄ μλ€κ³ λλΆλΆ μκ³ κ³μ λ°μ.
μ¬μ€ μ§μ λ€μ΄κ°μ 보μλ©΄ _heapify_max κ°μ ν¨μκ° μμ΄μ!
from heapq import *
λ‘ λΆλ¬μ€μλ©΄ μ¬μ©μ΄ λΆκ°λ₯νμ§λ§,
from heapq import _heapify_max
λ‘ λΆλ¬μ€μλ©΄ μΈ μλ μμ΅λλ€. (λ¬Όλ‘ λ¬Έμ ν λ μ°μ§ μμ΅λλ€..)
νμ΄μ¬μμ μμ ν privateλ μλ΅λλ€ ^μ€^
for k in range(W+1): | ||
if k == 0: | ||
if plum == 1: | ||
DP[time][1][k] = DP[time-1][1][k]+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.
μ¬κΈ°μ κΆκΈνκ² μλλ° DP[ i ] [ 0 ] [ k ] μλ μ²μμ μ΄κΈ°νν κ° 0μ΄ λ€μ΄μλ κ²μ΄ λ§λμ? μλλ무 μμΉκ° 1,2λ² λ°μ μμΌλ κ°λ μ±μ μν΄ 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.
ν μ νν©λλ€.
λλ¬΄κ° DPλΏλ§ μλλΌ DFS
, BFS
μλ μΌλΆλ‘ μλ£κ΅¬μ‘°μ 1κ°λ₯Ό λ μ μΈνκ³ 0λ² μΈλ±μ€λ₯Ό λΉμλλ μ‘κΈ°μ μ λ§μ΄ μ΄λ΅λλ€.
μ½μ§ν Code#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T, W;
cin >> T >> W;
vector<int> trees(T);
for (int &tree: trees) {
cin >> tree;
}
int cur = 0;
while (cur < T && trees[cur] == 1) {
cur++;
}
int prevTree = 1;
int count = 0;
vector<int> sortedCounts;
for (; cur < T; cur++) {
if (trees[cur] == prevTree) {
count++;
continue;
}
if (count) {
sortedCounts.emplace_back(count);
}
count = 1;
prevTree = trees[cur];
}
if (count) {
sortedCounts.emplace_back(count);
}
sort(sortedCounts.rbegin(), sortedCounts.rend());
int answer = T;
for (int i = 0; i < ((int) sortedCounts.size() - W + 1) / 2; i++) {
int discount = sortedCounts.back();
sortedCounts.pop_back();
answer -= discount;
}
cout << answer;
return 0;
} 7 2 κ° μ«μκ° μ°μλ νμκ° 1 2 2 2 μ΄λ―λ‘ μ μΌ νμκ° μ μ 1, μ¦ μ²«λ²μ§Έ 2λ₯Ό κ±°λ₯Έλ€. μ΄λ° λ‘μ§μΌλ‘ μ κ·Όνλλ° 2%μμ ν리λ€μ μλ§ νμκ° κ°μ λμ μ΄λ€ λ°©μμΌλ‘ μΈλ±μ€λ₯Ό λΉκ΅ν μ§ μ ν΄λμ§ μμμ κ·Έλ° κ² κ°μλ° νκ·λ PR μ λ νκ³ λ¬Έμ νμ΄κ°λλ€. |
π λ¬Έμ λ§ν¬
μλλ무
βοΈ μμλ μκ°
20λΆ
μ΄λ² μλ§ 2μ°¨ μ½λ© ν μ€νΈμμ μ΄μ λΉμ·ν λ¬Έμ κ° λμλ€.
μ΄ λ¬Έμ μμλ μλλ₯Ό λ΄λ λ°κ΅¬λκ° 1κ°,
κ·Έλ¦¬κ³ μμ§μ΄λ νμμ μ νμ΄ μμ§λ§,
μλ§μμλ λ°κ΅¬λκ° 2κ°μ μμ§μ΄λ νμ μ νμ΄ μμλ€. (맀 μ΄λ§λ€ μ΅λ 1μΉΈμ©λ§ μμ§μΌ μ μλ μ νμ΄ μμ)
μ½ν λΉμμλ λ무 볡μ‘ν΄ λ³΄μ¬μ λκ²Όλ λ¬Έμ μΈλ°..
μ§κΈ μμ λ€μ νμ΄λ³΄λ λ무λλ νμ΄κ° λͺ ννλ€.
λ무 μμ½λ€.... γ γ γ γ γ γ γ γ γ γ γ (κ²°κ³Όλ 3μ 6μΌμ..)
β¨ μλ μ½λ
μΌλ¨ μ΄ λ¬Έμ λ₯Ό μνμΌλ‘ μ κ·Όνλ €κ³ νλ€λ©΄,
μ΅λ 1000μ΄μ μκ°μμ 맀 μ΄λ§λ€ μμ§μ΄λλ, μμμ§μ΄λλμ μ νμ§ 2κ°κ° μμΌλ―λ‘
κ·Έλ κΈ° λλ¬Έμ μ΄ λ¬Έμ λ 보μλ§μ DPλ₯Ό μ΄μ©ν΄μΌκ² λ€λ μκ°μ΄ λ²μ© λλλ°,
DPλ₯Ό νλ €κ³ ν λμλ "κ³Όμ° μ΄λ€ μ 보λ₯Ό μ μ₯νλ©΄ λ΄κ° μ΄μ μ νλ κ³μ°λ€μ λ νμ§ μμ μ μμκΉ?"
λΌλ λ§μκ°μ§μΌλ‘ νμ΄μΌ νλ€.
μ¦, λ¬Έμ κ° λ§μ½ 1μ°¨μ DPλ¬Έμ λΌλ©΄
DP[i]
μμ iλ²μ§Έμ μ΅μ μ ν΄λ₯Ό μ μ₯ν΄μΌ νλ κ²μ΄κ³ ,2μ°¨μ DPλ¬Έμ λΌλ©΄
DP[i][j]
μμ iλ²μ§Έ jμμ μ΅μ μ ν΄λ₯Ό κ²μ 차곑차곑 μμμΌ νλ κ²μ΄λ€.κ·ΈλΌ λ¨Όμ μ΄ λ¬Έμ μμ κΈ°μ΅ν΄μΌ ν μ λ³΄κ° λ¬΄μμΈμ§ μκ°ν΄λ³΄μ.
μ΄ λ¬Έμ λ
i
μ΄μj
μμΉμ μμ κ²½μ° λ°μ μ μλ μλμ μ΅λ κ°―μλ₯Ό μ μ₯νλ©΄ λλ€.κ·Όλ° μ΄ λ¬Έμ λ μΆκ°μ μΌλ‘ μμ§μ΄λ νμμ μ νμ΄ μμΌλ―λ‘,
i
μ΄μ λ§μ§λ§μj
μμΉμ μμΌλ©΄μ νμ¬κΉμ§k
λ² μμ§μμ λ λ°μ μ μλ μλμ μ΅λ κ°―μλ₯Ό μ μ₯νλ€.μ¦, μ΄ λ¬Έμ λ 3μ°¨μ DPλ‘ λ¬Έμ λ₯Ό μ κ·Όν΄μΌ νλ€.
(DPμ μ΅μνμ§ μμλ€λ©΄ μ΄λ¬ν DPμ μ¬κ³ μ μ κ·Όμ΄ μ΄λ €μΈ μ μλ€. κ·Έλμ μ΄ λ¬Έμ κ° κ³¨λ 5μΈ μ μμ μ‘°κΈ κ°Έμ°λ± νλ€.)
μ΄ν΄λ₯Ό λκΈ°μν΄ μ΄λ―Έμ§ μλ£λ₯Ό μΆκ°νλ €κ³ νμΌλ..
3μ°¨μ DPλ₯Ό μ΄λ»κ² κ·Έλ €μΌ ν μ§ λ§λ§ν΄μ 첨κ°νμ§ λͺ»νλ€.
κ°λ¨νκ²λΌλ λ¬Έμ μ μ£Όμ΄μ§ μμλ‘ μ€λͺ νμλ©΄,
νμ¬ 6μ΄λΌκ³ μκ°νκ³ , λ€μ 7μ΄κ° λ λ λ°μ μ μλ μλμ μ΅λμ κ°―μλ₯Ό ꡬνλ €κ³ νλ€.
λ§μ½ μ μν©μμ 7μ΄μ 1λ² λλ¬΄λ‘ λ¨μ΄μ§λ μλλ₯Ό λ°κΈ° μν΄μ , μλμ κ°μ΄ κ³μ°ν΄μ£Όλ©΄ λλ€.
κ·ΈλΌ 7μ΄μ 2λ² λ무μ μλ κ²½μ°λ?
μλμ κ°λ€.
κ·Έλ¦ΌμΌλ‘ 3μ°¨μμ νννκΈ° νλ€μ΄μ
DP[i][j]
λ§ νννμ§λ§,μ¬κΈ°μ μμ§μ΄λ νμκΉμ§
DP[i][j][k]
λ₯Ό κΈ°λ‘νλ©΄μ λͺ¨λ μννλ©΄μ΅λ 1000μ΄ * μ΅λ 30λ² μμ§μ * λ무 2κ°
μ΄λ―λ‘ 6λ§ λ²μ μ°μ°μΌλ‘ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€.
DPμ μ¬κ³ λ₯Ό λν μ μλ μ’μ λ¬Έμ λΌκ³ μκ°λλ€.
π μλ‘κ² μκ²λ λ΄μ©