회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
c 회전하는큐 뭐가 문제인지 모르겠습니다...
1021번 - 회전하는 큐
redhound012
2년 전
0
예제와 게시판의 반례들 모두 맞게 나오는데 제출하면 바로 틀렸다고 나옵니다.
#include <stdio.h> #include <stdlib.h> #include <string.h> int *queue; int n; int m; int front; int rear; void EnqueueRear(int x) { rear = (rear + 1) % n; queue[rear] = x; } void EnqueueFront(int x) { queue[front] = x; if (front == 0) { front = n - 1; } else { front = front - 1; } } int DequeueRear() { int tmp = queue[rear]; queue[rear] = 0; if (rear == 0) { rear = n - 1; } else { rear = rear - 1; } return tmp; } int DequeueFront() { int tmp = queue[(front + 1) % n]; queue[(front + 1) % n] = 0; front = (front + 1) % n; return tmp; } void OperationI() { DequeueFront(); } void OperationII() { int tmp = DequeueFront(); EnqueueRear(tmp); } void OperationIII() { int tmp = DequeueRear(); EnqueueFront(tmp); } void Calc(int target, int *cnt) { int targetIdx = 0; for (int i = 0; i < n; i++) { if (queue[i] == target) targetIdx = i; } int distanceF = 0; int distanceR = 0; if ((front + 1) % n > targetIdx) { distanceF = n - (front + 1) + targetIdx; } else distanceF = targetIdx - front - 1; if (targetIdx > rear) { distanceR = n - targetIdx + rear; } else { distanceR = rear - targetIdx; } if (distanceF == 0) { OperationI(); return; } else if (distanceR == 0) { OperationIII(); (*cnt)++; OperationI(); return; } if (distanceF <= distanceR) { while (targetIdx != (front + 1) % n) { OperationII(); (*cnt)++; } OperationI(); } else { while (targetIdx != rear) { OperationIII(); (*cnt)++; } OperationIII(); (*cnt)++; OperationI(); } } int main() { n = 0, m = 0; scanf("%d %d", &n, &m); n = n + 1; queue = malloc(sizeof(int) * n); front = rear = 0; for (int i = 0; i < n; i++) { queue[i] = i; if (i != 0) rear++; } int target = 0; int cnt = 0; for (int i = 0; i < m; i++) { scanf("%d", &target); Calc(target, &cnt); } printf("%d\n", cnt); free(queue); } /* 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 1 1 3 4 5 6 7 8 9 10 10 1 3 4 5 6 7 8 9 9 10 1 3 4 5 6 7 8 8 10 1 3 4 5 6 7 7 6 5 */
댓글을 작성하려면
로그인
해야 합니다.
redhound012 2년 전
예제와 게시판의 반례들 모두 맞게 나오는데 제출하면 바로 틀렸다고 나옵니다.