Tenka 1 computer contest C-align

Source: Internet
Author: User
C-align

Time Limit: 2sec/memory limit: 1024 MB

Score:400Points

Problem Statement

You are givenNIntegers;I-Th of them isAI. Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints
  • 2 ≤N≤ 105
  • 1 ≤AI≤ 109
  • All values in input are integers.
Input

Input is given from standard input in the following format:

NA1:AN
Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample input 1 copy
568123
Sample output 1 copy
21

When the integers are arranged3, 8, 1, 6, 2, The sum of the absolute differences between the adjacent elements is| 3? 8 | + | 8? 1 | + | 1? 6 | + | 6? 2 | = 21. This is the maximum possible sum.

Sample input 2 Copy
6314159
Sample output 2 Copy
25
Sample input 3 copy
3551
Sample output 3 copy
8

Question: it is divided into odd and even numbers. When the value is an odd number, the two numbers in the middle must be at the left and right sides (in both cases) to maximize the result. The same is true for even numbers. There is only one case for better enumeration.

# Include <iostream> # include <cstring> # include <algorithm> # include <cmath> # include <cstdio> # include <vector> # include <queue> # include <set> # include <map> # include <stack> # define ll long // # define localusing namespace STD; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const double Pi = ACOs (-1.0); const int maxn = 1e5 + 10; int main () {# ifdef local if (freopen ("/users/Andrew/desktop/data.txt", "r", St Din) = NULL) printf ("can't open this file! \ N "); # endif int N; int A [maxn]; ll MX = 0; ll sum [maxn]; scanf (" % d ", & N ); for (INT I = 0; I <n; ++ I) {scanf ("% d", A + I) ;}sort (A, A + n ); for (INT I = 0; I <n; ++ I) {If (! I) sum [I] = A [I]; if (I) sum [I] = sum [I-1] + A [I];} If (N & 1) {If (n = 3) {MX = max (ABS (2 * A [2]-A [1]-A [0]), ABS (2 * A [0]-A [1]-A [2]); // when n = 3, special discussion} else {// If two numbers on the left are selected as two endpoints, they must be smaller than their adjacent numbers. // Similarly, if two numbers on the left are selected as two endpoints, they must be greater than their adjacent numbers. // Calculate the number of times X 2 MX = max (ABS (2 * (sum [n-1]-sum [n/2]) -2 * (sum [n/2-2])-(A [n/2] + A [n/2-1]), ABS (2 * (sum [n-1]-sum [n/2 + 1])-2 * (sum [n/2-1]) + (A [n/2] + A [n/2 + 1]);} else {MX = ABS (2 * (sum [n-1]-sum [n/2])-2 * (sum [n/2-2]) + ABS (A [n/2]-A [n/2-1]);} printf ("% LLD \ n", MX ); # ifdef local fclose (stdin); # endif return 0 ;}
View code
 

 

 



Tenka 1 computer contest C-align

Related Article

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.