Search
Duplicate
๐Ÿฅˆ

์Šคํƒ ์ˆ˜์—ด

์ฃผ์ฐจ
14
๋ฌธ์ œ๋ฒˆํ˜ธ
1874
์–ธ์–ด
Python
ํ‹ฐ์–ด
์‹ค๋ฒ„
์œ ํ˜•
์ž๋ฃŒ๊ตฌ์กฐ
nj_Blog
O
nj_์ƒํƒœ
์™„๋ฃŒ
์ดํ•ด๋„
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ๋งํฌ

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

Code

๋ฉ”๋ชจ๋ฆฌ : 143944 KB
์‹œ๊ฐ„ : 32 ms
N = int(input()) arr=[] result=[] temp=[] for _ in range(N): arr.append(int(input())) j = 0 for i in range(1, N+1): temp.append(i) result.append('+') while (temp and temp[-1] == arr[j]): temp.pop() j += 1 result.append('-') if not temp: # ๋น„์–ด ์žˆ์œผ๋ฉด for i in result: print(i) else: print("NO")
Python
๋ณต์‚ฌ
์‹œ๊ฐ„์ดˆ๊ณผ
#include <iostream> #include <vector> int N; std::vector<int> arr; std::vector<char> result; std::vector<int> temp; void input_faster() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); } void input() { std::cin >> N; for(int i = 0 ; i < N ; i++){ int a; std::cin >> a; arr.push_back(a); } } void solve() { int j = 0; for (int i = 1 ; i < N + 1 ; i++){ temp.push_back(i); result.push_back('+'); while (!temp.empty() && temp.back() == arr[j]){ temp.pop_back(); j++; result.push_back('-'); } } } void print_val() { if (temp.empty()){ for (int i = 0 ;i < result.size();i++) std::cout << result[i] << std::endl; } else std::cout << "NO"; } int main() { input_faster(); input(); solve(); print_val(); return (0); }
C++
๋ณต์‚ฌ

๋ฉ”๋ชจ

โ€ข
๋ฌธ์ œ ์ดํ•ด๋„ ํ•œ์ฐธ ๊ฑธ๋ฆผ
โ†’ N์ด push๋˜๋ฉด 1, 2, 3, 4, 5, 6, ,,,,, N ์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด์˜ฌ ๋•Œ ์ฃผ์–ด์ง„ ์ˆ˜์—ด (ex. 43687521) ์ด ์ˆœ์„œ๋Œ€๋กœ pop ๋˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ!
โ€ข
๋ฌธ์ œ ์ ‘๊ทผ ์ˆœ์„œ
์กฐ๊ฑด : j = 0
1.
๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ 1 ~ N ๊นŒ์ง€ temp ๋ฐฐ์—ด์— push ๋ฅผ ํ•˜๋‹ค๊ฐ€
2.
temp์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ˆซ์ž์™€ arr[j] ์˜ ์ˆ˜์™€ ๊ฐ™์œผ๋ฉด pop โ†’ j++ ์„ ํ•˜๋ฉด์„œ ๊ฐ™์ง€ ์•Š์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
3.
result ๋ฐฐ์—ด โ†’ push ํ•  ๋•Œ '+', popํ•  ๋•Œ '-' ๋„ฃ๊ธฐ
4.
์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚ฌ์„ ๋•Œ temp ๋ฐฐ์—ด์ด
โ†’ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์ˆ˜์—ด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ NO ์ถœ๋ ฅ
โ†’ ๋น„์–ด์žˆ์œผ๋ฉด result ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ์ค„๋ฐ”๊ฟˆ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ถœ๋ ฅ