首页 > 代码库 > ZOJ 3798 Abs Problem(规律题)
ZOJ 3798 Abs Problem(规律题)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798
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; }
ZOJ 3798 Abs Problem(规律题)