ZOJ 3798 Abs Problem(找规律)

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 2
2 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值