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
Sample Output
【题解】
题意就是给一个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代码】
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; }
相关文章推荐
- 组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)
- ZOJ 3798 Abs Problem(规律题)
- zoj 3798 Abs Problem( ZOJ Monthly, August 2014 - A)
- ZOJ 3798 Abs Problem
- ZOJ 3798 Abs Problem
- ZOJ 3798 Abs Problem
- ZOJ-3798-Abs Problem
- zoj3798 Abs Problem 水题
- zoj 3798 Abs Problem(数学:推理+滚动数组)
- zoj 3785 What day is that day?(找规律,KMP)
- zoj 3785 What day is that day? (打表找规律)
- zoj 3702 Gibonacci number 找规律
- HDOJ 3788 zoj问题【规律题】
- ZOJ 3789 Abs Problem
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- zoj 2060 Fibonacci Again(fibonacci数列规律、整除3的数学特性)
- ZOJ2686 Cycle Game (博弈,找规律,搜索)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ 3886 Nico Number(线段树单点更新+找规律)
- ZOJ 2686(规律博弈)