HDU 1214 round-table conference

Source: Internet
Author: User
Round-table Conference

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3046 accepted submission (s): 2123


Members of the problem descriptionhdu ACM training team often discuss their problems during summer training. every time they face problems that they cannot solve, they will sit around a circular table and communicate with each other. After discussion, they generally cannot solve the problems, this is only a round-table meeting exclusive to the hdu acm training team. One day you can come and try it out. :) during the discussion, Eddy came up with an extremely strange idea, if they exchange seats for a pair of adjacent ACM players in each minute, how long will it take for them to obtain the order of seats in the opposite state? (That is, for each team member, the former team member on his left is later on his right, and the former team member on his right is on his left), of course it is difficult to find other smart team members, I will solve this odd problem right away. Do you know how to solve it?

 

Input for a given number of N (1 <= n <= 32767), it indicates that there are n people, and how long it takes to obtain the order of seats (reverse) opposite to the original state (for each person, the person on the left was on the right, and the person on the right was on the left.

 

Output outputs a row of data, indicating the time required (in minutes)

 

Sample input456

 

Sample output2 4 6 problem solving report (mathematical problem): according to the requirements of the question, the round-table team members should be inverted. First of all, I thought of the steps required to directly find 1-N in reverse order by Bubble sorting, but the results are different from the answers given. Later I thought about it. All people can enclose a circle and divide the circle into two segments. For example, when n = 6, after 1 to 3 is backward, It is 3, 2, and 1, then, 6 to 6 are sorted in descending order of 6, 5, and 4, so that the last circle is 3, 2, 1, 6, 5, and 4, meeting the requirements. Therefore, to minimize the last time, you only need to divide N into the closest two segments and use the Bubble Method to reverse the order. (Note: the complexity of the bubble sort algorithm is N * (n-1)/2)
 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4  5 int main() 6 { 7     int n, n1, n2, sum; 8     while (scanf("%d",&n) != EOF) 9     {10         sum = 0;11         if(n%2 == 0)12         {13             n1 = n/2;14             n2 = n/2;15         }16         else17         {    18             n1 = n/2;19             n2 = n-n1;20         }21         sum = n1*(n1-1)/2 + n2*(n2-1)/2;22         printf("%d\n",sum);23     }24     return 0;25 }
View code

 

 

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.