组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

Abs ProblemTime 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 thanN. 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 numbera1 fromthe N integers, and Bob will write it down on the first paper, that’sb1. Then in the following kth rounds, Alice will choose a numberak (2 ≤ k ≤ N), then Bob will write the number bk=|ak-bk-1| on thekth paper. |x| means the absolute value ofx.

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 containingN 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 Input2Sample Output1 11 22 1

链接:click here~~

题意:Alice和Bob玩数字游戏, 这次Alice和Bob玩的是绝对值游戏. (Alice和Bob以前只玩博弈类游戏, 现在开始玩数列了,,^_^)   Alice有n个数(分别是1~n), 然后Alice随机从N个数中取数, 组成一个数列{A(i)}.   Bob来构建{B(i)}数列, 满足如下规则: B(1) = A(1) B(i) = | A(i) – B(i-1) | 即取绝对值 (i>=2 && i<=n)  目标是: 所有序列中B(n)的最小值和最大值, 并且构造它们的一种可能序列.

【解题思路】  这种题目,可以猜测该题是规律题, 那么我们来找找规律, 看看其满足什么样的规律?  枚举2, 3, 4的情况  1). 当n=2时, (1, 2)的最小值为1(2, 1), 最大值为1(2, 1) ,注意 B(i) = | A(i) – B(i-1)|  2). 当n=3时, (1, 2, 3)的最小值为0(3, 2, 1), 最大值为2(1, 2, 3)  3). 当n=4时, (1, 2, 3, 4)的最小值为0(4, 3, 2, 1), 最大值为4(3, 2, 1, 4)  要构造一个输出序列,{n, n-1, …, 4, 3, 2, 1}, 观察得这必然是最小的序列之一  再来分析奇偶性,可以发现, 奇数时B(n)最小值为1, 偶数时B(n)最小值为0  ps :同时我们可以大胆猜测, B(n)最大取值 = n – B(n – 1)的最小值, 即 序列{ B(n) }={ 最小值 B(n -1 )序列, n },暂时证明略~~

代码:

#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>using namespace std;int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int judge(int n){if(((n+1)/2)&1) return 1;else return 0;}void solve(int n){printf("%d %d\n",judge(n),n-judge(n-1));for(int i=n;i>1;i–){printf("%d ",i);}printf("1\n");for(int i=n-1;i>0;i–){printf("%d ",i);}printf("%d\n",n);}int main(){int n;while(cin>>n){solve(n);}return 0;}

你在雨中行走,你从不打伞,你有自己的天空,它从不下雨。

组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

相关文章:

你感兴趣的文章:

标签云: