您的位置:首页 > 其它

ZOJ 3798 Abs Problem(找规律)

2017-08-15 20:18 399 查看
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 1
1 22 1


【题解】

 题意就是给一个n,代表有1~n个数,现在有一些空白卡片,现在要从这1~n个数中拿出一个数来写在第 1 张卡片上,剩下的卡片,每次从剩下的数中拿一个,按照|ai - b(k-1)|的规则运算后写在卡片上,每个数只能用一次,求最后卡片n上的最小的和最大的数。

 

先手写找规律,发现每四个数可以抵消,所以输入的数mod 4 后就只在1~3范围内了,直接找规律打表,最小值就是a[n%4]。

至于最大值,就是n减去最小值了,maxn=a[(n-1)%4],为什么是n-1呢,因为mod值打表下标范围是从0~3,没有4,所以减1再mod,符合范围了,比如n=5,如果mod4,值是1,因该是表中的第一个数,但是表中第一个数下标是0,所以给5减1在去模就好了。

 

【AC代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
const int N=50005;
int m,n;
int a[4]={0,1,1,0};

int main()
{

while(~scanf("%d",&m))
{
int maxn=m-a[(m-1)%4];
int minn=a[m%4];
cout<<minn<<" "<<maxn<<endl;

cout<<m;
for(int i=m-1;i>0;--i)
cout<<" "<<i;
cout<<endl<<m-1;
for(int i=m-2;i>0;--i)
cout<<" "<<i;
cout<<" "<<m<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: