码迷,mamicode.com
首页 > 其他好文 > 详细

组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

时间:2015-04-05 20:30:12      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:zoj   比赛   

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 fromthe 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 thekth 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

链接:click here~~

题意:Alice和Bob玩数字游戏, 这次Alice和Bob玩的是绝对值游戏. (Alice和Bob以前只玩博弈类游戏, 现在开始玩数列了,^_^)
     Alice有n个数(分别是1~n), 然后Alice随机从N个数中取数, 组成一个数列{A(i)}.
     Bob来构建{B(i)}数列, 满足如下规则:
           B(1) = A(1)    
           B(i) = | A(i) - B(i-1) |    即取绝对值 (i>=2 && i<=n)
  目标是:   所有序列中B(n)的最小值和最大值, 并且构造它们的一种可能序列.

【解题思路】
  这种题目,可以猜测该题是规律题, 那么我们来找找规律, 看看其满足什么样的规律?
  枚举2, 3, 4的情况
  1). 当n=2时, (1, 2)的最小值为1(2, 1), 最大值为1(2, 1) ,注意   B(i) = | A(i) - B(i-1)|
  2). 当n=3时, (1, 2, 3)的最小值为0(3, 2, 1), 最大值为2(1, 2, 3)
  3). 当n=4时, (1, 2, 3, 4)的最小值为0(4, 3, 2, 1), 最大值为4(3, 2, 1, 4)
  要构造一个输出序列,{n, n-1, ..., 4, 3, 2, 1}, 观察得这必然是最小的序列之一
  再来分析奇偶性,可以发现, 奇数时B(n)最小值为1, 偶数时B(n)最小值为0
  ps :同时我们可以大胆猜测, B(n)最大取值 = n - B(n - 1)的最小值, 即 序列{ B(n) }={ 最小值 B(n -1 )序列, n },暂时证明略~~

代码:

#include <iostream>
#include <stdio.h>
#include  <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int judge(int n)
{
    if(((n+1)/2)&1) return 1;
    else return 0;
}
void solve(int n)
{
    printf("%d %d\n",judge(n),n-judge(n-1));
    for(int i=n;i>1;i--)
    {
        printf("%d ",i);
    }
    printf("1\n");
    for(int i=n-1;i>0;i--)
    {
        printf("%d ",i);
    }
    printf("%d\n",n);
}
int main()
{
    int n;
    while(cin>>n)
    {
        solve(n);
    }
    return 0;
}


组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

标签:zoj   比赛   

原文地址:http://blog.csdn.net/u013050857/article/details/44890555

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!