2019 Dec Bronze Problem 3 Livestock Lineup: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
== Official Problem Statement == | |||
[http://www.usaco.org/index.php?page=viewproblem2&cpid=965 Livestock Lineup] | |||
==Problem Statement== | ==Problem Statement== | ||
Line 39: | Line 43: | ||
[[Category:Yearly_2019_2020]] | [[Category:Yearly_2019_2020]] | ||
[[Category:Bronze]] | [[Category:Bronze]] | ||
[[Category:Simulation]] | |||
[[Category:Brute Force]] | |||
[[Category:Sorting]] |
Latest revision as of 23:01, 11 June 2023
Official Problem Statement[edit]
Problem Statement[edit]
The USACO 965 problem, Livestock Lineup, is a classic problem of sorting. Given a list of N (1 ≤ N ≤ 10,000) cows, each with a unique ID number from 1 to N, you must arrange them in a line such that the absolute difference between the ID numbers of any two adjacent cows is as small as possible.
Solution[edit]
The solution to this problem is to sort the cows in ascending order according to their ID numbers. This can be done using a C++ implementation of the quicksort algorithm.
// C++ implementation of quicksort algorithm
void quicksort(int arr[], int left, int right) {
int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; /* recursion */ if (left < j) quicksort(arr, left, j); if (i < right) quicksort(arr, i, right);
}