Abs Problem
Problem Description
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 a 1 a_1 a1 from the N integers, and Bob will write it down on the first paper, that’s b 1 b_1 b1. Then in the following k-th rounds, Alice will choose a number a k a_k ak (2 ≤ k ≤ N), then Bob will write the number b k = ∣ a k − b k − 1 ∣ b_k=|a_k-b_{k-1}| bk=∣ak−bk−1∣ on the k-th paper. |x| means the absolute value of x.
Now Alice and Bob want to kown, what is the maximum and minimum value of b N b_N 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 1
1 2
2 1
题意
1~n共n个数,开始选一个数,作为 b 1 b_1 b1,接下来选n-1次,第k次选时, b k = ∣ a k − b k − 1 ∣ b_k=|a_k-b_{k-1}| bk=∣ak−bk−1∣ 。每个数只能选一次,求 b n b_n bn的最大值和最小值,并给出对应选取顺序的序列。
题解:
先考虑最小的,相邻的两个差为1,则考虑逆序,依次选n到1,若n%4余数为1或2,则最小值为1,余数为3或0,则最小值为0。
最大值:保证最后一次选取的数最大,然后令前面的值最小即可。选取方案为:n-1,n-2…1, n。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1020;
const int mod = 1000000009;
int main()
{
int n, m, i, j, k, ans1, ans2;
while(~scanf("%d", &n))
{
if(n%4 == 1)ans1 = 1, ans2 = n;
else if(n%4 == 2)ans1 = 1, ans2 = n-1;
else if(n%4 == 3)ans1 = 0, ans2 = n-1;
else ans1 = 0, ans2 = n;
printf("%d %d\n", ans1, ans2);
for(i=n;i>1;i--)printf("%d ", i);
printf("1\n");
for(i=n-1;i>0;i--)printf("%d ", i);
printf("%d\n", n);
}
return 0;
}