Skip to content

Conversation

@hadongun
Copy link
Collaborator

@hadongun hadongun commented May 2, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

(https://www.acmicpc.net/problem/10773)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์Šคํƒ ๋ฌธ์ œ๊ฐ€ ํ’€๊ณ  ์‹ถ์–ด ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.
์ฒซ ์ค„์— ์ •์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ์ดํ›„ K์ค„์— ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ์ •์ˆ˜๊ฐ€ 0์ด๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ์“ด ์ˆ˜๋ฅผ ์ง€์›Œ์•ผ ํ•˜๋ฏ€๋กœ
์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๊ฐ€ 0์ด๋ฉด pop()์„ ์‹คํ–‰ํ•œ๋‹ค. ์ด๋ฅผ K๋ฒˆ ๋ฐ˜๋ณตํ•˜๋„๋ก ๋ฐ˜๋ณต๋ฌธ์„ ์„ค์ •

K๋ฒˆ์˜ ๋ฐ˜๋ณต์ด ๋๋‚˜๋ฉด ์Šคํƒ ๋‚ด๋ถ€์— ๋‚จ์•„์žˆ๋Š” ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด stack์ด ๋นŒ ๋•Œ ๊นŒ์ง€ pop์„ ํ•ด์ฃผ๋ฉฐ ๊ทธ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•ด์ฃผ์–ด ์ถœ๋ ฅํ•œ๋‹ค.

์ˆ˜๋„ ์ฝ”๋“œ
import sys ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜จ๋‹ค.
๋นˆ ์Šคํƒ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค
์ž…๋ ฅ ๊ฐœ์ˆ˜ k๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค

k๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:
    ์ •์ˆ˜ N์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค

    N์ด 0์ด ์•„๋‹ˆ๋ฉด:
        ์Šคํƒ์— N์„ ์ถ”๊ฐ€ํ•œ๋‹ค

    N์ด 0์ด๋ฉด:
        ์Šคํƒ์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค (pop)

ํ•ฉ๊ณ„๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ Sum๊ณผ ์ž„์‹œ ๋ณ€์ˆ˜ temp๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค

์Šคํƒ์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:
    ์Šคํƒ์—์„œ ๊ฐ’์„ ํ•˜๋‚˜ ๊บผ๋‚ธ๋‹ค (pop)
    Sum์— ๊ทธ ๊ฐ’์„ ๋”ํ•œ๋‹ค

Sum์„ ์ถœ๋ ฅํ•œ๋‹ค

์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•ด์„œ ๋‹ค์Œ ์Šคํƒ ๋ฌธ์ œ๋Š” ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๋กœ ๊ฐ€์ ธ์˜ฌ๊ฒŒ์šฅ..^<^

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

์Šคํƒ ๊ธฐ๋ณธ ๋ฌธ์ œ๋Š” ์ด์ œ ๋„ˆ๋ฌด ๋งŽ์ด ํ’€์—ˆ์ฃ 
์Šฌ ์ต์ˆ™ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด ๋ฐฉ์‹์€ ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ๋žŒ๋“ค์ด ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
์ž…๋ ฅ๋˜๋Š” ์ˆซ์ž๋ฅผ ์Šคํƒ์— push ํ•˜๋‹ค๊ฐ€, ์ž…๋ ฅ ๊ฐ’์ด "0"์ผ ๊ฒฝ์šฐ pop ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์Šคํƒ์— ๋‚จ์€ ์ˆ˜๋“ค์„ ํ•ฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

C++
#include <iostream>
#include <stack>
using namespace std;

int main() {
    int K;
    cin >> K;
    
    stack<int> stack;
    while(K--) {
        int N;
        cin >> N;
        
        stack.push(N);
        if(N == 0) {
            stack.pop();
            stack.pop();
        }
    }
    
    int sum = 0;
    while(!stack.empty()) {
        sum += stack.top();
        stack.pop();
    }
    
    cout << sum << '\n';
    
    return 0;
}

์ด์ œ ์Šคํƒ ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„๋ฅผ ๋†’์—ฌ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
์†”์งํžˆ ์‹ค๋ฒ„ 4 ~ 5 ์Šคํƒ ๋ฌธ์ œ ๋งŽ์ด ํ’€์—ˆ๋”ฐ ใ…‡ใ…ˆ?

๋‹ค์Œ์—๋Š” ๊ณจ๋“œ ๋ฌธ์ œ๋กœ ๋„์ „~
์‹œ๊ฐ„ ์กฐ๊ธˆ๋งŒ ์“ฐ๋ฉด ํ’€ ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”! ํ‹ฐ์–ด๋„ ๊ธ‰์ƒ์Šนํ•˜๊ณ  ๊ฐœ๊ฟ€~
์˜คํฐ์ˆ˜, ํƒ‘, ๊ด„ํ˜ธ์˜ ๊ฐ’

@froglike6
Copy link
Collaborator

์ €๋„ ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ , deque๋ฅผ ์‚ฌ์šฉํ• ๊นŒ ํ•˜๋‹ค๊ฐ€ list์™€ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์•„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. pop๊ณผ append๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Python3

import sys
input=sys.stdin.readline
stack=[]
for _ in range(int(input())):
    num = int(input())
    if num==0:
        stack.pop()
    else:
        stack.append(num)
        
print(sum(stack))

Copy link
Collaborator

@caucsejunseo caucsejunseo left a comment

Choose a reason for hiding this comment

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

์ต์ˆ™ํ•œ ๋ง›, ๋Š˜๋จน๋˜ ๋ง›์ด๋ผ์„œ ์ข‹๋„ค์š” ใ…Žใ…Ž
ezํ•˜๊ฒŒ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

C

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


int main() {

    int N = 0;
    scanf("%d", &N);

    int* stack = (int*)malloc(sizeof(int) * N);
    int input = 0;
    int top = 0;
    long long sum = 0;

    for (int i = 0; i < N; i++)
    {
        scanf("%d", &input);
        if (input == 0)
        {
            if (top == 0)
            {
                continue;
            }
            else
            {
                top--;
                stack[top] = NULL;
            }
        }
        else
        {
            stack[top++] = input;
        }
    }

    for (int i = 0; i < top; i++)
    {
        sum = stack[i] + sum;
    }
    printf("%d", sum);

    return 0;
}

Co-authored-by: Do Hyeonseok <[email protected]>
@hadongun hadongun merged commit 9120dc3 into AlgoLeadMe:main May 8, 2025
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.

4 participants