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

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;
}
时间: 2024-09-30 22:40:01

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

ZOJ 3798 Abs Problem(规律题)

题目链接: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

ZOJ 3798 Abs Problem

找规律.... 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

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

UVA 1363 Joseph&#39;s Problem 找规律+推导 给定n,k;求k%[1,n]的和。

/** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想法才明白. 我一开始往素数筛那种类似做法想. 想k%[1,n]的结果会有很多重复的,来想办法优化. 但没走通. 果然要往深处想. 通过观察数据发现有等差数列.直接观察很难确定具体规律:此处应该想到用式子往这个方向推导试一试. lrj想法: 设:p = k/i; 则:k%i = k-i*p; 容易想到

ZOJ 3789 Abs Problem

有时候像这种题,没有明显的思路,感觉像规律题.那么先暴力打表,再找规律就很快了. 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 先上我的暴力打表,这种肯定是TLE的,只用它发现规律就好了. #include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f #define N 100 us

zoj Abs Problem

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.

ZOJ3798 Abs Problem

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 学会了对于序列用next_permutation暴力打表 可以先打表找规律 #include<cstdio> #include<algorithm> #define INF 0x3f3f3f3f// #define N 100 using namespace std; int seq[N];//保存当前序列 int mi[N], ma[N];//

ZOJ 3768Continuous Login(找规律然后二分)

Continuous Login Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Pierre is recently obsessed with an online game. To encourage users to log in, this game will give users a continuous login reward. The mechanism of continuous log

FOJ有奖月赛-2016年8月 Problem A Daxia &amp; Wzc&#39;s problem(找规律)

Problem A Daxia & Wzc's problem Accept: 42    Submit: 228Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题: Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d; 首先让Daxia求出数列A(0)前n项和,得到新数列A(1); 然后让Daxia求出数列A(