Abs Problem

来源:互联网 发布:多益网络徐宥箴微博 编辑:程序博客网 时间:2024/06/12 01:10
Abs Problem

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Alice and Bob is playing a game, and this time the game is all about the absolute value!

Alice has N different positive integers, and each number is not greater than N. Bob has a lot of blank paper, and he is responsible for the calculation things. The rule of game is pretty simple. First, Alice chooses a number a1 from the N integers, and Bob will write it down on the first paper, that's b1. Then in the following kth rounds, Alice will choose a number ak (2 ≤ k ≤ N), then Bob will write the number bk=|ak-bk-1| on the kth paper. |x| means the absolute value of x.

Now Alice and Bob want to kown, what is the maximum and minimum value of bN. And you should tell them how to achieve that!

Input

The input consists of multiple test cases;

For each test case, the first line consists one integer N, the number of integers Alice have. (1 ≤ N ≤ 50000)

Output

For each test case, firstly print one line containing two numbers, the first one is the minimum value, and the second is the maximum value.

Then print one line containing N numbers, the order of integers that Alice should choose to achieve the minimum value. Then print one line containing N numbers, the order of integers that Alice should choose to achieve the maximum value.

Attention: Alice won't choose a integer more than twice.

Sample Input

2

Sample Output

1 11 22 1

Author: ZHANG, Ruixiang

Source: ZOJ Monthly, August 2014

题目大意: 输入N,序列a是1~N任意序的序列,序列b={bk | bk=|ak-bk-1|,k∈[1,N]},求bn能取得最大值和最小值,并求其对应的序列a?当 N =2时 最小  1  2最大  1  2那么N=3呢!对于最大我们很容易得到  N = 2 最小后加上 N 就OK了。即 :最大  1 2 3那么最小怎么得到了呢! 难道是在  N =2 最大值后加上 N 构成1 2 3 吗?显然不是不然就跟最小的不就一样了。但可以肯定的就是即使不是也有一定的联系毕竟是最大的减去最大的对吧!这么考虑下当 N =2时最大  1  2  可以看成是 N = 1 的最小值后加上 N 构成1 2所以 当 N = 2 时最大 1  2 的 2 前面的数是由N = 1最小确定的也是确定的,可以看成 X那么到底怎么得到 N = 3时的最小呢!由上面的推导我们可以将当N = 2的最大改为  X  2  即  X  N-1考怒这两组数据X   N-1  N     得到 X+1X    N  N-1    得到 X-1 很显然 最小的应该是  X-1依次推下去便得到这样的关系:N 的最大值来自 N-1 的最小值后加上 NN 的最小值来自 N-1 的最大值前N-2 个数加上 N 再加上 N-1  即:X   N-1  N还有就是: N=2 时最小  1  2最大  2  1按照这种关系往下推也能得出正解,比赛时就是取的这个初始值。可能是因为最大时 1 2 和2 1都相等吧!我也解释不清。看了队友的方法:最小值就倒序   最大值就N前面数倒序然后在其后加上N其实我试了下,最小值也可以在队友的前一组数的最大值序的 N-2 位置 插入 N 也即 X  N  N-1总之 X为 N-2 的最小序,X  N  N-1  就满足 N序的最小序最大序就是 N-1 的最小序后加 N最小序0——>0     最小序2——>0 2 1     最小序4——>0 2 1 4 3  最小序1——>0 1   最小序3——>0 1 3 2   最小序5——>0 1 3 2 5 4
#include<cstdio>#include<iostream>#include<iterator>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<queue>#include<stack>#include<list>#include<map>#include<set>#include<algorithm>using namespace std;#define inf 1<<29int a[50010], b[50010];int main(){int n, Min, Max;while (scanf("%d", &n) != EOF){Min = 0; Max = 0;a[1] = 1; a[2] = 2;b[1] = 2; b[2] = 1;for (int i = 3; i <= n; i++){if (i % 2) { a[i] = i;  b[i] = b[i - 1];  b[i - 1] = i; }else       { b[i] = i;  a[i] = a[i - 1];  a[i - 1] = i; }}for (int i = 1; i <= n; i++){Min = abs(a[i] - Min);Max = abs(b[i] - Max);}if (n % 2 == 0){printf("%d %d\n", Min, Max);for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", a[i]);else printf("%d ", a[i]);}for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", b[i]);else printf("%d ", b[i]);}}else{printf("%d %d\n", Max, Min);for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", b[i]);else printf("%d ", b[i]);}for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", a[i]);else printf("%d ", a[i]);}}}return 0;}/*#include<cstdio>#include<iostream>#include<iterator>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<queue>#include<stack>#include<list>#include<map>#include<set>#include<algorithm>using namespace std;#define inf 1<<29int a[50010], b[50010];int main(){int n, Min, Max;a[1] = 2; a[2] = 1;b[1] = 1; b[2] = 3; b[3] = 2;for (int i = 4; i <= 50000; i++){if (i % 2) { b[i - 1] = i;  b[i] = i - 1; }else       { a[i - 1] = i;  a[i] = i - 1; }}while (scanf("%d", &n) != EOF){Min = 0; Max = 0;for (int i = 1; i <= n; i++){if (i == n) Max = abs(i - Max);if (n % 2 == 0){Min = abs(a[i] - Min);if(i<n) Max = abs(b[i] - Max);}else{Min = abs(b[i] - Min);if (i < n) Max = abs(a[i] - Max);}}printf("%d %d\n", Min, Max);if (n % 2 == 0){for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", a[i]);else printf("%d ", a[i]);}for (int i = 1; i <= n; i++){if (i == n) printf("%d\n",n);else printf("%d ", b[i]);}}else{for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", b[i]);else printf("%d ", b[i]);}for (int i = 1; i <= n; i++){if (i == n) printf("%d\n", n);else printf("%d ", a[i]);}}}return 0;}*/



0 0