Baekjoon 2493
in Study / Computer science on Baekjoon 2493
Description
KOI 통신연구소는 레이저를 이용한 새로운 비밀 통신 시스템 개발을 위한 실험을 하고 있다. 실험을 위하여 일직선 위에 N개의 높이가 서로 다른 탑을 수평 직선의 왼쪽부터 오른쪽 방향으로 차례로 세우고, 각 탑의 꼭대기에 레이저 송신기를 설치하였다. 모든 탑의 레이저 송신기는 레이저 신호를 지표면과 평행하게 수평 직선의 왼쪽 방향으로 발사하고, 탑의 기둥 모두에는 레이저 신호를 수신하는 장치가 설치되어 있다. 하나의 탑에서 발사된 레이저 신호는 가장 먼저 만나는 단 하나의 탑에서만 수신이 가능하다.
예를 들어 높이가 6, 9, 5, 7, 4인 다섯 개의 탑이 수평 직선에 일렬로 서 있고, 모든 탑에서는 주어진 탑 순서의 반대 방향(왼쪽 방향)으로 동시에 레이저 신호를 발사한다고 하자. 그러면, 높이가 4인 다섯 번째 탑에서 발사한 레이저 신호는 높이가 7인 네 번째 탑이 수신을 하고, 높이가 7인 네 번째 탑의 신호는 높이가 9인 두 번째 탑이, 높이가 5인 세 번째 탑의 신호도 높이가 9인 두 번째 탑이 수신을 한다. 높이가 9인 두 번째 탑과 높이가 6인 첫 번째 탑이 보낸 레이저 신호는 어떤 탑에서도 수신을 하지 못한다.
탑들의 개수 N과 탑들의 높이가 주어질 때, 각각의 탑에서 발사한 레이저 신호를 어느 탑에서 수신하는지를 알아내는 프로그램을 작성하라.
Input
첫째 줄에 탑의 수를 나타내는 정수 N이 주어진다. N은 1 이상 500,000 이하이다. 둘째 줄에는 N개의 탑들의 높이가 직선상에 놓인 순서대로 하나의 빈칸을 사이에 두고 주어진다. 탑들의 높이는 1 이상 100,000,000 이하의 정수이다.
Output
첫째 줄에 주어진 탑들의 순서대로 각각의 탑들에서 발사한 레이저 신호를 수신한 탑들의 번호를 하나의 빈칸을 사이에 두고 출력한다. 만약 레이저 신호를 수신하는 탑이 존재하지 않으면 0을 출력한다.
Example I & O
Input
5
6 9 5 7 4
Output
0 0 2 2 4
My solution
typedef struct node
{
int index;
int data;
} Node;
Node* createNode(int index ,int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->index = index;
return newNode;
}
void destroyNode(Node** node)
{
free(*node);
*node = NULL;
}
typedef struct stack
{
int capacity;
int top;
Node** nodeList;
} Stack;
void createStack(Stack** stack, int capacity)
{
*stack = (Stack*)malloc(sizeof(Stack));
(*stack)->capacity = capacity;
(*stack)->top = -1;
(*stack)->nodeList = (Node**)malloc(sizeof(Node*) * capacity);
}
int isEmpty(Stack* stack)
{
return (stack->top == -1);
}
void push(Stack* stack, Node* node)
{
stack->top++;
stack->nodeList[stack->top] = node;
}
Node* pop(Stack* stack)
{
Node* poped = stack->nodeList[stack->top--];
return poped;
}
Node* getTop(Stack* stack)
{
return stack->nodeList[stack->top];
}
void destroyStack(Stack** stack)
{
free((*stack)->nodeList);
(*stack)->nodeList = NULL;
free(*stack);
*stack = NULL;
}
int main()
{
Stack* stack = NULL;
Stack* tempStack = NULL;
int N = 0;
scanf("%d", &N);
int* result = (int*)malloc(sizeof(int) * N);
memset(result, 0, sizeof(int) * N);
createStack(&stack, N+1);
createStack(&tempStack, N+1);
for (int i = 0; i < N; i++)
{
int temp = 0;
scanf("%d", &temp);
Node* newNode = createNode(i + 1, temp);
push(stack, newNode);
}
push(tempStack, pop(stack));
while (!isEmpty(stack))
{
Node* compareElement = pop(stack);
while (!isEmpty(tempStack) && compareElement->data > getTop(tempStack)->data)
{
Node* poped = pop(tempStack);
result[poped->index - 1] = compareElement->index;
}
push(tempStack, compareElement);
}
for (int i = 0; i < N; i++)
{
if (i == N-1)
{
printf("%d", result[i]);
}
else
{
printf("%d ", result[i]);
}
}
destroyStack(&stack);
destroyStack(&tempStack);
free(result);
result = NULL;
}
- Sketch
- Prepare 2 Stacks (stack, tempStack)
If the input is 6 9 5 7 4
| | | | | 4 | | | | 7 | | | | 5 | | | | 9 | | | | 6 | | | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 0, 0, 0]| | | | | | | | | 7 | | | | 5 | | | | 9 | | | | 6 | | 4 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 0, 0, 0]7 > 4 (top of tempStack) | | | | | | | | | | | | | 5 | | | | 9 | | | | 6 | | 4 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 0, 0, 0]7 > 4 (top of tempStack) | | | | | | | | | | | | | 5 | | | | 9 | | | | 6 | | 7 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 0, 0, 4 (place num of 7)]5 < 7 (top of tempStack) | | | | | | | | | | | | | | | | | 9 | | 5 | | 6 | | 7 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 0, 0, 4]9 > 5 (top of tempStack) | | | | | | | | | | | | | | | | | | | | | 6 | | 7 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 2 (place num of 9), 0, 4]9 > 7 (top of tempStack) | | | | | | | | | | | | | | | | | | | | | 6 | | 9 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 2, 2 (place num of 9), 4]6 < 9 (top of tempStack) | | | | | | | | | | | | | | | | | | | 6 | | | | 9 | |_______________| |_______________| stack tempStack 6 9 5 7 4 Result = [0, 0, 2, 2, 4] (Exit)
- Code Analysis
typedef struct node
{
int index;
int data;
} Node;
Node* createNode(int index ,int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->index = index;
return newNode;
}
void destroyNode(Node** node)
{
free(*node);
*node = NULL;
}
node: elements of stack which have 2 information (the input number, the place num of that number)
Instead of using dictionary for linking the input number to the place num, I make node thorugh “struct”
typedef struct stack
{
int capacity;
int top;
Node** nodeList;
} Stack;
void createStack(Stack** stack, int capacity)
{
*stack = (Stack*)malloc(sizeof(Stack));
(*stack)->capacity = capacity;
(*stack)->top = -1;
(*stack)->nodeList = (Node**)malloc(sizeof(Node*) * capacity);
}
int isEmpty(Stack* stack)
{
return (stack->top == -1);
}
void push(Stack* stack, Node* node)
{
stack->top++;
stack->nodeList[stack->top] = node;
}
Node* pop(Stack* stack)
{
Node* poped = stack->nodeList[stack->top--];
return poped;
}
Node* getTop(Stack* stack)
{
return stack->nodeList[stack->top];
}
void destroyStack(Stack** stack)
{
free((*stack)->nodeList);
(*stack)->nodeList = NULL;
free(*stack);
*stack = NULL;
}
Implementation of Stack
int main()
{
Stack* stack = NULL;
Stack* tempStack = NULL;
int N = 0;
scanf("%d", &N);
int* result = (int*)malloc(sizeof(int) * N);
memset(result, 0, sizeof(int) * N);
createStack(&stack, N+1);
createStack(&tempStack, N+1);
for (int i = 0; i < N; i++)
{
int temp = 0;
scanf("%d", &temp);
Node* newNode = createNode(i + 1, temp);
push(stack, newNode);
}
////
}
2 Stacks Initialization
result array initialization through memset
Formation of Node elements that gonna enter the stacks
int main()
{
////
push(tempStack, pop(stack));
while (!isEmpty(stack))
{
Node* compareElement = pop(stack);
while (!isEmpty(tempStack) && compareElement->data > getTop(tempStack)->data)
{
Node* poped = pop(tempStack);
result[poped->index - 1] = compareElement->index;
}
push(tempStack, compareElement);
}
///
}
Before the loop, top of stack has to shift to tempStack
See the Sketch
int main()
{
////
for (int i = 0; i < N; i++)
{
if (i == N-1)
{
printf("%d", result[i]);
}
else
{
printf("%d ", result[i]);
}
}
destroyStack(&stack);
destroyStack(&tempStack);
free(result);
result = NULL;
}