Minimum and maximum differences

2.8

32 votes
Basic Programming, , Basics of Implementation, Greedy Algorithms
Problem

You are given a number n. Your task is to determine two arrays of permutation of size n so that aii for each 1in and the sum of |aii| for each 1in is minimum and maximum possible.

Example 

If n=2, then for the minimum difference is 2 and the corresponding array permutation is {2,1}|21|+|12|=2 and the maximum difference is also 2 and the corresponding array permutation is {2,1}|21|+|12|=2.

Input format

The first line contains an integer n (1n4×104).

Output format

  • In the first line, print one integer for the minimum difference.
  • In the second line, print permutation {a1,a2,,an} corresponding to the minimum difference.
  • In the third line, print one integer for the maximum difference.
  • In the fourth line, print permutation {b1,b2,,bn} corresponding to the maximum difference

If there is no permutation for minimum, then print  1  instead of the first and second lines.
If there is no permutation for maximum, then print 1  instead of the third and fourth lines.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

given n=3 so if we take {3,1,2} as our permuatation for minimum we will get |31|+|12|+|23|=2+1+1=4  as sum of absolute diffrence with index. you can't get absolute difference less than this without  violatiiong condintion aii

for maximum differance we can use {2,3,1} as permutation for maximum. we will get |21|+|32|+|13|=1+1+2=4 as sum of absolute diffrence with index. you  can't get absolute difference more than this without violatiiong condintion aii

In this case (n=3) minimum and maximum differance is same

Editor Image

?