Uva11714-blind sorting (inference)

Source: Internet
Author: User

Question Link


Question:Returns n numbers and obtains the maximum number of comparisons between the maximum number and the second largest number.

Ideas:We can compare the two data pairs by using a binary tree. The maximum value of the two numbers is equivalent to that of the two parent nodes. Therefore, we can know that the maximum value is n-1, then, assuming that all the left sons are greater than the right sons, the right son with the maximum value has the second largest number compared with the value in the left subtree, and the number of comparisons is the tree height.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; int N; int main () {While (scanf ("% d", & N )! = EOF) {int ans = n-1 + (INT) (Ceil (log (N)/log (2)-1; printf ("% d \ n ", ans);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.