Soldier standing problem, soldier standing

Source: Internet
Author: User

Soldier standing problem, soldier standing
Soldiers standing[Problem description]On a grid-based playground, n soldiers stood on the grid in disorder. The grid points are represented by integer coordinates (x, y. Soldiers can move one step along the edge, bottom, left, and right of the grid, but only one soldier can be on any grid point at the same time. According to the officer's command, the soldiers should be neatly arranged into a horizontal queue, that is, arranged into (x, y), (x + 1, y ),..., (X + n-1, y ). How can we select the values of x and y so that soldiers can rank in a column with the minimum number of moving steps.[Programming task]Calculates the minimum number of steps required to arrange all soldiers in a row.[Input format]Input data is provided by file sol. in. The first row of the file is the number of soldiers n, 1 ≤ n ≤ 1st. The next n rows are the initial positions of soldiers. Each row has two integers x and y,-10000 ≤ x, and y ≤ 10000.[Output format]When the program runs, the computing result is output to the file sol. out. The number of rows in the file is the minimum number of moving steps required by soldiers to form a row.[Input example]51 22 21 33-23 3

 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 int tot; 6 int a[10001]; 7 int b[10001]; 8 int d; 9 int main()10 {11     int n;12     cin>>n;13     for(int i=1;i<=n;i++)14     {15         cin>>a[i];16         cin>>b[i];17     }18     sort(a+1,a+n+1);19     sort(b+1,b+n+1);20     for(int i=1;i<=n;i++)21     {22         a[i]=a[i]-i+1;23     }24     sort(a+1,a+n+1);25     int mid=b[n/2+1];26     int mida=a[n/2+1];27     for(int i=1;i<=n;i++)28     {29         tot=tot+abs(b[i]-mid);30         tot=tot+abs(a[i]-mida);31     }32     cout<<tot;33     return 0;34 }

 

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.