Programming

[6์ฃผ์ฐจ] ๋ฐฑ์ค€ 1874๋ฒˆ: ์Šคํƒ ์ˆ˜์—ด_C์–ธ์–ด

mnzy๐ŸŒฑ 2022. 5. 16. 15:07

๋ฐฑ์ค€ 1874: ์Šคํƒ ์ˆ˜์—ด

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

 

1874๋ฒˆ: ์Šคํƒ ์ˆ˜์—ด

1๋ถ€ํ„ฐ n๊นŒ์ง€์— ์ˆ˜์— ๋Œ€ํ•ด ์ฐจ๋ก€๋กœ [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ˆ˜์—ด [4, 3, 6, 8, 7, 5, 2, 1]์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net


- ์ˆ˜์—ด์„ ์ด๋ฃจ๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜ n์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

- ๊ทธ ๋‹ค์Œ ์ค„ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ 1์ด์ƒ n ์ดํ•˜์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (์ค‘๋ณต๋˜๋Š” ์ˆ˜๋Š” ์—†์Œ)

- ์ž…๋ ฅ๋œ ์ˆ˜์—ด์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์—ฐ์‚ฐ์„ ํ•œ ์ค„์— ํ•œ ๊ฐœ์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

//push= +, pop = -

- ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋ฉด push์™€ pop์„ ํ‘œํ˜„ํ•˜๊ณ  ์•ˆ๋œ๋‹ค๋ฉด "NO"๋ผ๊ณ  ํ‘œ์‹œํ•œ๋‹ค.


์†”์งํžˆ ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ๋„ˆ๋ฌด ์ดํ•ด๊ฐ€ ์•ˆ๋ผ์„œ ๊ณ„์† ์ฝ์—ˆ๋‹ค.

์Šคํƒ ์ˆ˜์—ด์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ž˜ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๊ณ ๋‚˜์„œ์•ผ ์ œ๋Œ€๋กœ ๋ฌธ์ œ๊ฐ€ ์ดํ•ด๊ฐ€ ๋˜์—ˆ๋‹ค!-.,-


[์ฐธ๊ณ ] https://wtg-study.tistory.com/53

- LIFO ๋ฐฉ์‹์— ์˜ค๋ฆ„์ฐจ์ˆœ์ธ ์Šคํƒ์€ 3๊ฐ€์ง€ ๊ทœ์น™์„ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋‹ค.

->์Šคํƒ์˜ ์ƒ๋‹จ ๋ถ€๋ถ„ ์ˆ˜๊ฐ€ ๋ชฉํ‘œ์ˆ˜๋ณด๋‹ค ๋‚ฎ์€ ๊ฒฝ์šฐ(push)

-> ์Šคํƒ์˜ ์ƒ๋‹จ ๋ถ€๋ถ„ ์ˆ˜์™€ ๋ชฉํ‘œ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ(pop)

-> ์Šคํƒ์˜ ์ƒ๋‹จ ๋ถ€๋ถ„ ์ˆ˜๊ฐ€ ๋ชฉํ‘œ์ˆ˜๋ณด๋‹ค ๋†’์€ ๊ฒฝ์šฐ(NO)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100000
 
char result[SIZE * 2];
int stack[SIZE];
int top = -1;
 
int main() {
    int n;
    scanf("%d", &n);

    int* arr = (int*)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);
 
    int num = 1;
    int idx = 0, result_idx = 0;
    while (1) {
        if (top == -1 || stack[top] < arr[idx]) {
            stack[++top] = num++;
            result[result_idx++] = '+';
        }

        else if (stack[top] == arr[idx]) {
            top--;
            result[result_idx++] = '-';
            idx++;
        }
   
        else {
            printf("NO\n");
            return 0;
        }
        if (result_idx == n * 2) break;
    }

    for (int i = 0; i < result_idx; i++)
        printf("%c\n", result[i]);
 
    return 0;
}

[์ถœ์ฒ˜] https://wtg-study.tistory.com/53