zoj3798Abs Problem(思维)

题目链接:

huangjing

题目意思:

用1~n中的数字进行组合,得到后面减前面的一项的最大最小值。。。

思路:

多试两个就会发现从n到1的排列得到的是最小值,同理从n-1到1得到的也是最小值,那么用n-这个最小值,那么必定得到的是最大值。。。

题目:

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

代码:

#include<cstdio>
#include<cmath>
const int maxn=50000+10;

int fabs(int x)
{
    if(x<0)  return -x;
    else return x;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int Min=n;
        for(int i=n-1;i>=1;i--)
            Min=fabs(i-Min);
        int Max=n-1;
        for(int i=n-2;i>=1;i--)
            Max=fabs(i-Max);
        Max=fabs(n-Max);
        printf("%d %d\n",Min,Max);
        for(int i=n;i>1;i--)
            printf("%d ",i);
        printf("%d\n",1);
        for(int i=n-1;i>=1;i--)
            printf("%d ",i);
        printf("%d\n",n);
    }
    return 0;
}

时间: 2024-10-11 11:29:57

zoj3798Abs Problem(思维)的相关文章

FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)

Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shortest closed path or circuit that visits every edge of a (connected) undirected graph. When the graph has an Eulerian Circuit (a closed walk that cover

D. Yet Another Subarray Problem 思维 难

D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: 首先枚举0~m,这个是说更新的位置,如果是1 当m==3 就更新1 4 7 10... 如果是2,当m==3 就更新 2 6 8 11.... 最后都会被更新的. 核心代码 for (int j = 0; j < n - i; ++j) { s += sum[j]; if (j % m == 0)

Codeforces A. Sweet Problem(思维题)

传送门 题意: 有红,绿,蓝三种颜色的糖各r,g,b个,每天要吃两个糖且颜色不同,问最多能吃多少天 思路: 先把r,g,b排序,从小到大为a,b,c如果a+b<=c,结果肯定输出a+b 否则输出(a+b+c)/2 原因:为了使天数最多,每次取最多和次多的两堆,当次多的变成和最少的相等时,把最多的那个平分个最少和中间那一堆,(如果最多的为奇数,那么总共还剩一个,如果为偶数,所有糖果用完),所以说最优解剩下的糖果不超过两个(a+b>c时) 代码 #include <iostream>

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

Problem 1577 - K-th character 小思维题 逆向查找

Problem 1577 - K-th character 题目抽象:给你一个字符串,给你一些操作,每个操作将子串[L,R]反转.所有操作完成后,询问一些位置上的字符. 思路:只需逆向查找就行了. 这样的小思维有时候却想不到. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 usin

Jam&#39;s math problem(思维)

Jam's math problem Submit Status Practice HDU 5615 Description Jam has a math problem. He just learned factorization.  He is trying to factorize  into the form of .  He could only solve the problem in which p,q,m,k are positive numbers.  Please help

863D - Yet Another Array Queries Problem(思维)

原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i], a[l]=a[r] 2 l r ,[l, r] 区间的数字位置反转. 若干个操作之后输出a[b[i]]. 思路: 由于是在操作结束后输出,且b[i]的个数不多(<=100),所以可以通过反推求出答案. AC代码: 1 #include<iostream> 2 #include<cs

hdu5107 K-short Problem(线段树+离散化+思维)

题目链接: huangjing 题意:就是给出狠点建筑的坐标和高度,然后给出很多询问,求在这个坐标右下角的第k矮的建筑.. 思路:太弱了我,这个题目从上个星期天就开始看,但是一直不会,所以只能看别人思路,因为那个k小于10,所以左右节点只取前十就可以了,但是我觉得万一不记录完全万一发生丢失怎么办,后来一想sb了,如果左右节点都取前10的话,那么根节点得到的20个值,在排序必定取到了前10啊...言归正传,这道题因为数据范围太大了,前对建筑和询问一起排序,按x从小到大,再按y从小到大,在按建筑优先

HDU 4972 A simple dynamic programming problem(数学思维题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4972 Problem Description Dragon is watching NBA. He loves James and Miami Heat. Here's an introduction of basketball game:http://en.wikipedia.org/wiki/Basketball. However the game in Dragon's version is