sunghyun1356   2년 전

계속 시간초과가 뜨네요

정렬을 안하고 바로 찾도록 만들어서 그런것일까요?

djm03178   2년 전

적혀있는 수는 -262보다 크거나 같고, 262보다 작거나 같기 때문에 22번째 줄처럼 쓸 수 없습니다.

sunghyun1356   2년 전

범위자체가 초과된다는 건가요?

B배열 인덱스로 A의 값을 넣는 방법말고 COUNT해주는 방법이 시간초과가 안된다는 말씀이실 까요?

sunghyun1356   2년 전

#include

#include

#include

long long card[1000];

void Swap(long long arr[], int idx1, int idx2)

{

int temp = arr[idx1];

arr[idx1] = arr[idx2];

arr[idx2] = temp;

}

int Partition(long long arr[], int left, int right)

{

int pivot = arr[left];

int low = left + 1;

int high = right;

while (low <= high)

{

while (pivot >= arr[low] && low <= right)

low++;

while (pivot <= arr[high] && high >= (left + 1))

high--;

if (low <= high)

Swap(arr, low, high);

}

Swap(arr, left, high);

return high;

}

void QuickSort(long long arr[], int left, int right)

{

if (left <= right)

{

int pivot = Partition(arr, left, right);

QuickSort(arr, left, pivot - 1);

QuickSort(arr, pivot + 1, right);

}

}

int main()

{

int n;

scanf("%d", &n);

for (int i = 0; i < n; i++)

{

scanf("%lld", &card[i]);

}

int cnt = 1, max = 0, index = 0;

QuickSort(card, 0, n);

for (int i = 0; i < n - 1; i++)

{

if (card[i] == card[i + 1])

{

cnt++;

if (cnt > max)

{

max = cnt;

index = i;

}

}

else

cnt = 1;

}

printf("%lld", card[index]);

}

QUICK정렬로 정렬한후에 인덱스로 넣는게 아니라 카운트 하는 방식으로 했는데도 시간초과가 나는데 혹시 도와주실 c고수분들 계실 까요?

djm03178   2년 전

퀵소트는 최악의 경우 O(N^2)이기 때문에 너무 오래 걸립니다.

댓글을 작성하려면 로그인해야 합니다.