From 46e4c3cfba7d1006cea3a796d3698a5f44deb21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=ED=98=84?= Date: Tue, 9 Jul 2024 12:04:59 +0900 Subject: [PATCH] [Silver II] Title: Pancake Deque, Time: 1592 ms, Memory: 232868 KB, Score: 25 point -BaekjoonHub --- .../Pancake\342\200\205Deque.py" | 38 +++++++++++++++++++ .../README.md" | 36 ++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 "\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/Pancake\342\200\205Deque.py" create mode 100644 "\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/README.md" diff --git "a/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/Pancake\342\200\205Deque.py" "b/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/Pancake\342\200\205Deque.py" new file mode 100644 index 0000000..b8e1602 --- /dev/null +++ "b/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/Pancake\342\200\205Deque.py" @@ -0,0 +1,38 @@ +import sys +input = sys.stdin.readline +from collections import deque + +def max_paying_customers(T, test_cases): + results = [] + for case_number in range(1, T + 1): + N, pancakes = test_cases[case_number - 1] + dq = deque(pancakes) + + max_deliciousness = -1 + paying_customers = 0 + + while dq: + if dq[0] <= dq[-1]: + pancake = dq.popleft() + else: + pancake = dq.pop() + + if pancake >= max_deliciousness: + paying_customers += 1 + max_deliciousness = pancake + + results.append(f"Case #{case_number}: {paying_customers}") + + return results + +t = int(input().strip()) +test_cases = [] + +for _ in range(t): + n = int(input().strip()) + pancakes = list(map(int, input().strip().split())) + test_cases.append((n, pancakes)) + +results = max_paying_customers(t, test_cases) +for result in results: + print(result) diff --git "a/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/README.md" "b/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/README.md" new file mode 100644 index 0000000..8e58992 --- /dev/null +++ "b/\353\260\261\354\244\200/Silver/25096.\342\200\205Pancake\342\200\205Deque/README.md" @@ -0,0 +1,36 @@ +# [Silver II] Pancake Deque - 25096 + +[문제 링크](https://www.acmicpc.net/problem/25096) + +### 성능 요약 + +메모리: 232868 KB, 시간: 1592 ms + +### 분류 + +자료 구조, 덱, 그리디 알고리즘 + +### 제출 일자 + +2024년 7월 9일 12:04:50 + +### 문제 설명 + +

Pancakes are normally served in stacks, but the Infinite House of Pancakes embraces change! The restaurant's new advertising hook is to serve the pancakes from a deque, or double-ended queue.

+ +

You are a server at the restaurant, and your job is to serve every pancake in the deque. Customers will arrive one at a time, and each one gets a single pancake. You must serve each customer either the leftmost or rightmost pancake in the deque; the choice is yours. When a pancake is served, it disappears from the deque, exposing the pancake that was next to it. Or, once there is only one pancake left, your only choice is to serve that one, and then your job is complete!

+ +

+ +

Each pancake has a deliciousness level. Because customers do not get to choose which pancakes they get, each customer only has to pay for their pancake if it is at least as delicious as each of the pancakes that all of the previous customers got. (The first customer always pays for their pancake, since in that case there are no previous customers.)

+ +

How many customers will pay for their pancake, if you serve the pancakes in an order that maximizes that number?

+ +### 입력 + +

The first line of the input gives the number of test cases, T. T test cases follow. Each test case is described with two lines. The first line of a test case contains a single integer N, the number of pancakes in the pancake deque. The second line of a test case contains N integers D1,D2,,DN, where Di is the deliciousness level of the i-th pancake from the left in the deque.

+ +### 출력 + +

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of customers who pay for their pancakes, if you serve the pancakes in an order that maximizes that number.

+