ZOJ 3798 Abs Problem(规律题)

79 篇文章 0 订阅

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798

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 1
1 2
2 1

Author:  ZHANG, Ruixiang
Source:  ZOJ Monthly, August 2014



这是个规律题,很容易可以发现说  n=3 或 4 时 minv=0; 另 k=n%4 则当 k=0或者3 时 minv=0 ,其他情况下minv=1(可由小数据推得)。

又易知maxv=n-minv(上一次的)   所以令 k=(n-1)%4  ,当k=0||k=3时 maxv=n,否则 maxv=n-1


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
void gao(int n){
    int k=n%4;
    int minv,maxv;
    if(k==0||k==3) minv=0;
    else minv=1;
    k=(n-1)%4;
    if(k==0||k==3) maxv=n;
    else maxv=n-1;
    cout<<minv<<" "<<maxv<<endl;
    for(int i=n;i>=1;i--){
        if(i==n) cout<<i;
        else cout<<" "<<i;
    }
    cout<<endl;
    for(int i=n-1;i>=1;i--){
        if(i==n-1) cout<<i;
        else cout<<" "<<i;
    }
    cout<<" "<<n<<endl;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        if(n==1){
            cout<<1<<" "<<1<<endl;
            cout<<1<<endl;
            cout<<1<<endl;
            continue;
        }
        if(n==2){
            cout<<1<<" "<<1<<endl;
            cout<<1<<" "<<2<<endl;
            cout<<2<<" "<<1<<endl;
            continue;
        }
        gao(n);
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值