Editing
2019 Dec Bronze Problem 3 Livestock Lineup
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Official Problem Statement == [http://www.usaco.org/index.php?page=viewproblem2&cpid=965 Livestock Lineup] ==Problem Statement== 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== 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); } [[Category:Yearly_2019_2020]] [[Category:Bronze]] [[Category:Simulation]] [[Category:Brute Force]] [[Category:Sorting]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information