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

1-YIM2UL2ET #3

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion YIM2UL2ET/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

| μ°¨μ‹œ | λ‚ μ§œ | λ¬Έμ œμœ ν˜• | 링크 | 풀이 |
|:----:|:---------:|:----:|:-----:|:----:|
| 1μ°¨μ‹œ | 2023.10.27 | BFS | - | - |
| 1μ°¨μ‹œ | 2023.10.27 | 그리디 | [BOJ 18310] | [BOJ 18310 풀이] |
---
37 changes: 37 additions & 0 deletions YIM2UL2ET/그리디/1μ°¨μ‹œ - BOJ 18310.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

int main(void)
{
int n, temp, index, result, up, down;
long long value, tvalue;
int villige[100000] = {0};
Copy link
Collaborator

Choose a reason for hiding this comment

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

list도 μ’‹μ§€λ§Œ vectorλ₯Ό μ‚¬μš©ν•΄λ³΄μ‹œλŠ” 것도 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

vectorλž‘ 반반으둜 μ‚¬μš©ν•˜κ³  있긴 ν•œλ° ν™•μ‹€νžˆ vectorκ°€ cpp κ³΅λΆ€ν•˜λ©΄ ν• μˆ˜λ‘ μž₯점이 더 λ§Žλ”λΌκ΅¬μš©, μ•žμœΌλ‘œ 자주 μ΄μš©ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€!


value = 0;
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> temp;
villige[temp-1]++;
value += temp-1;
}
Comment on lines +11 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

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

βœ”οΈ villige의 값이 0이면 λ„˜μ–΄κ°€κ²Œ μ½”λ“œκ°€ μž‘μ„±λ˜μ–΄μžˆλŠ”λ° μ§‘μ˜ μœ„μΉ˜(μž…λ ₯κ°’)을 list에 λ‹΄κ³ , 이λ₯Ό μ •λ ¬ν•˜μ—¬ μ‚¬μš©ν•˜λŠ” 방법도 ν•œλ²ˆ μƒκ°ν•΄λ³΄μ…¨μœΌλ©΄ μ’‹κ² μŠ΅λ‹ˆλ‹€.

λ©”λͺ¨λ¦¬μ™€ μ‹œκ°„μ„ 쑰금 더 μ•„λ‚„ 수 μžˆμ„ 것 κ°™μ•„μš”!!


result = 1, index = 0, up = n - villige[0], down = villige[0], tvalue = value;
// tvalue: μž„μ‹œ value, up: 봐야할 μ§‘μ˜ 수, down: μ§€κΈˆκΉŒμ§€ 봐온 μ§‘μ˜ 수

for (int i = 1; i < 100000; i++) { // λ§ˆμ„ λŒμ•„λ³΄κΈ°
if (villige[i] == 0) continue; // i+1번째 μœ„μΉ˜μ— 집이 μ—†μœΌλ©΄ λ‹€μŒ μœ„μΉ˜λ‘œ 이동

tvalue += down * (i - index);
tvalue -= up * (i - index);
down += villige[i];
up -= villige[i];

if (tvalue < value) { // μ§€κΈˆ κ³„μ‚°ν•œ valueκ°€ μ €μž₯ν–ˆλ˜ value보닀 μž‘μœΌλ©΄
value = tvalue; // μ§€κΈˆ κ³„μ‚°ν•œ valueκ°’ μ €μž₯.
result = i+1; // μ •λ‹΅μœΌλ‘œ i+1번째 μœ„μΉ˜ μ €μž₯
}
index = i; // 졜근 봀던 μ§‘μ˜ μœ„μΉ˜λ₯Ό κΈ°μ–΅ν•˜κΈ°
}

std::cout << result;
return 0;
}