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-Dolchae #2

Merged
merged 3 commits into from
Nov 9, 2023
Merged

1-Dolchae #2

merged 3 commits into from
Nov 9, 2023

Conversation

Dolchae
Copy link
Collaborator

@Dolchae Dolchae commented Nov 8, 2023

πŸ”— 문제 링크

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

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

1μ‹œκ°„

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

문제 μ„€λͺ…


- 문제λ₯Ό μ΄ν•΄ν•˜λŠ” 데에 쑰금 어렀움이 μžˆμ—ˆλŠ”λ°, 이 문제 쑰건의 핡심은 μŠ€νƒμ— pushν•  λ•Œ λ°˜λ“œμ‹œ 'μ˜€λ¦„μ°¨μˆœ'으둜만 κ°€λŠ₯ν•˜λ‹€λŠ” 것이닀. 예λ₯Ό λ“€μ–΄ 3을 pushν•˜κ³  μ‹Άλ‹€λ©΄ 1,2,3이 μŠ€νƒ μ•ˆμ— μžˆμ–΄μ•Ό ν•œλ‹€. - 또 λ‹€λ₯Έ 쑰건은 popλ˜λŠ” μˆœμ„œκ°€ μž…λ ₯된 μˆœμ„œμ™€ κ°™μ•„μ•Ό ν•œλ‹€λŠ” 것이닀.

μ½”λ“œ μ„€λͺ…

n = int(input())
stack = []
ans = []
cur = 1
k=1
  • n으둜 μˆ˜μ—΄μ˜ 크기λ₯Ό λ°›λŠ”λ‹€.
  • push와 popκ°€ μ΄λ€„μ§ˆ stack λ¦¬μŠ€νŠΈμ™€ pushλŠ” '+', pop은 '-'둜 λ‚˜νƒ€λ‚΄ λ§ˆμ§€λ§‰ 좜λ ₯에 μ‚¬μš©λ  ans 리슀트λ₯Ό λ§Œλ“ λ‹€.
  • 1λΆ€ν„° μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μŠ€νƒμ„ 받을 수 있게 cur λ³€μˆ˜λ₯Ό λ§Œλ“€κ³ , 문제 쑰건에 따라 좜λ ₯이 λΆˆκ°€λŠ₯ν•œ μˆ˜μ—΄μΈ 경우 k=0, κ°€λŠ₯ν•œ 경우 k=1둜 λ§Œλ“€κΈ° μœ„ν•œ λ³€μˆ˜(k)λ₯Ό λ§Œλ“ λ‹€.

for _ in range(n):
    input_num = int(input())
    while cur <= input_num:
        stack.append(cur)
        ans.append("+")
        cur += 1

    if stack[-1] == input_num:
        stack.pop()
        ans.append("-")
    else:
        k = 0
  • for문을 μ΄μš©ν•΄ input_num에 숫자λ₯Ό μ €μž₯ν•œλ‹€.
  • μ˜€λ¦„μ°¨μˆœμ„ μ§€μΌœμ•Ό ν•˜λ―€λ‘œ curκ°€ input_numκ³Ό κ°™μ•„μ§ˆ λ•ŒκΉŒμ§€ stack에 pushν•˜κ³  ans에 '+'ν•˜κ³ , λ§ˆμ§€λ§‰μœΌλ‘œ curλ₯Ό 1μ”© μ¦κ°€μ‹œν‚¨λ‹€.
  • λ§Œμ•½ stack의 λ§ˆμ§€λ§‰ μš”μ†Œμ™€ input_num이 κ°™μ•„μ‘Œλ‹€λ©΄ 즉 λͺ©ν‘œν•˜λŠ” 숫자만큼 stack에 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ €μž₯λ˜μ—ˆλ‹€λ©΄ stack에 popν•˜κ³  ans '-' ν•œλ‹€.
  • while문을 λΉ μ Έλ‚˜μ˜¨ 후에도 stack λ§ˆμ§€λ§‰ μš”μ†Œ == input_num이 μ•ˆλœλ‹€λ©΄ 문제 쑰건에 λΆ€ν•©ν•˜μ§€ λͺ»ν•˜λŠ” μˆ˜μ—΄μΈ κ²ƒμ΄λ―€λ‘œ k=0으둜 λ°”κΎΌλ‹€.
if k==1:
    for i in ans:
        print(i)
else:
    print("NO")
  • kκ°€ μ΄ˆκΈ°μ„€μ •κ°’μΈ 1κ³Ό κ°™λ‹€λ©΄ (else문이 μž‘λ™ν•˜μ§€ μ•Šμ•„ λ³€ν™”ν•˜μ§€ μ•Šμ•˜λ‹€λ©΄) ans 리슀트λ₯Ό 좜λ ₯ν•œλ‹€.
  • kκ°€ 0이라면 (λ³€ν™”ν–ˆλ‹€λ©΄) "NO"λ₯Ό 좜λ ₯ν•œλ‹€.

- 전체 μ½”λ“œμž…λ‹ˆλ‹€.
n = int(input())
stack = []
ans = []
cur = 1
k=1
for _ in range(n):
    input_num = int(input())
    while cur <= input_num:
        stack.append(cur)
        ans.append("+")
        cur += 1

    if stack[-1] == input_num:
        stack.pop()
        ans.append("-")
    else:
        k = 0
if k==1:
    for i in ans:
        print(i)
else:
    print("NO")

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@tgyuuAn tgyuuAn removed the request for review from gjsk132 November 8, 2023 13:00
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

고생 λ§Žμ•˜μŠ΅λ‹ˆλ‹€!
싀버 2 λ‚œμ΄λ„λΌ κ½€ 어렀웠을 텐데 1μ‹œκ°„λ™μ•ˆ 끈질기게 λΆ™μž‘μ•˜λ‹€λ‹ˆ... 이미 μƒμœ„ 1%μž…λ‹ˆλ‹€ 😎

Comment on lines +8 to +15
while cur <= input_num:
stack.append(cur)
ans.append("+")
cur += 1

if stack[-1] == input_num:
stack.pop()
ans.append("-")
Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 이전에 문제λ₯Ό ν’€μ—ˆμ—ˆλŠ”λ°, 문제(더 λ‚˜μ•„κ°€ μŠ€νƒ)μ—μ„œ μ˜λ„ν•œ λŒ€λ‘œ 잘 ν‘Έμ‹  것 κ°™μŠ΅λ‹ˆλ‹€!
μ €μ˜ κ²½μš°μ—λŠ” μˆ˜μ—΄μ„ λ¨Όμ € μ €μž₯해놓고, 1λΆ€ν„° nκΉŒμ§€ 숫자λ₯Ό λ„£μœΌλ©΄μ„œ κ·Έ λ•Œλ§ˆλ‹€ μˆ˜μ—΄μ„ λ‹€μ‹œ 확인해 ν‘ΈλŠ” μ‹μœΌλ‘œ ν–ˆκ±°λ“ μš”.

vector<int> sequence(n);
for (int& i : sequence) 
  cin >> i;

vector<int> stack;
vector<char> operators;
for (int i = 1; i <= n; i++)  {
  stack.emplace_back(i);
  operators.emplace_back('+');

  while (!stack.empty() && stack.back() == sequence.front())  {
    stack.pop_back();
    operators.emplace_back('-');
    sequence.erase(sequence.begin());
  }
}
  • 1λΆ€ν„° nκΉŒμ§€ 숫자λ₯Ό μŠ€νƒμ— ν•˜λ‚˜μ”© λ„£κ³ , κ·Έ λ•Œλ§ˆλ‹€ μ €μž₯된 μˆ˜μ—΄μ„ μ•žμ—μ„œλΆ€ν„° 확인해 μΌμΉ˜ν•˜λŠ” μˆ«μžκ°€ 없을 λ•ŒκΉŒμ§€ stackκ³Ό sequenceμ—μ„œ κ·Έ 숫자λ₯Ό μ—†μ• λŠ” μ‹μœΌλ‘œ μˆœνšŒν–ˆμ£ .
  • λ‹€ λŒμ•˜μ„ λ•Œ μŠ€νƒμ— λ‚¨μ•„μžˆλŠ” μˆ«μžκ°€ μžˆλ‹€λ©΄ λ§Œλ“€ 수 μ—†λŠ” μˆ˜μ—΄μ΄λΌ νŒλ‹¨ν•˜λŠ” μ‹μœΌλ‘œ 말이죠.

ans.append("-")
else:
k = 0
if k==1:
Copy link
Collaborator

Choose a reason for hiding this comment

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

kλ₯Ό μ΄μš©ν•΄ ν•΄λ‹Ή μˆ˜μ—΄μ„ λ§Œλ“€ 수 μžˆλŠ” 지 μ—†λŠ” 지λ₯Ό κ΅¬λΆ„μ§“λŠ”λ°, 그런 μš©λ„λΌλ©΄ boolean λ³€μˆ˜λ₯Ό μ΄μš©ν•˜λŠ” 것이 μ’€ 더 직관적이지 μ•Šμ„κΉŒμš”?
예λ₯Ό λ“€μ–΄

isPossibleSequence = True

...

else:
  isPossibleSequence = False
  break

이런 μ‹μœΌλ‘œ ν•˜λ©΄ λˆˆμ— ν™• λ“€μ–΄μ˜€κ² μ£ ? λ˜ν•œ λΆˆκ°€λŠ₯ν•œ μˆ˜μ—΄μž„μ΄ ν™•μΈλœ μˆœκ°„ μ¦‰μ‹œ break ν•΄μ£Όλ©΄ 쑰금 더 효율적일 것 κ°™λ„€μš”!

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

.

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

λ¬Έμ œκ°€ 이해가 잘 μ•ˆλ˜μ–΄μ„œ 였래 걸리고 μ–΄λ €μ› λ„€μš”...
μ„€λͺ…을 λ³΄λ‹ˆ 이해가 잘 λ˜μ—ˆμŠ΅λ‹ˆλ‹€!
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!!πŸ˜ŠπŸ‘

@9kyo-hwang 9kyo-hwang merged commit 40ec044 into main Nov 9, 2023
@9kyo-hwang 9kyo-hwang mentioned this pull request Feb 11, 2024
@Dolchae Dolchae mentioned this pull request Feb 19, 2024
@Dolchae Dolchae mentioned this pull request Mar 4, 2024
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