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];//保存最小值最大值相应序列
int calc(int n)//总排序数
{
    int p = 1;
    for (int i = 1; i <= n; ++i)
        p *= i;
    return p;
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        for (int i = 1; i <= n; ++i)
            seq[i] = i;
        int k = calc(n), MIN = INF, MAX = -INF;
        for (int i = 0; i < k; ++i)
        {
            int b;
            next_permutation(seq + 1, seq + n + 1);
            for (int i = 1; i <= n; ++i)
            {
                if (i == 1) b = seq[i];
                else b = abs(b - seq[i]);
            }
            if (b <= MIN)
            {
                MIN = b;
                for (int i = 1; i <= n; ++i)
                    mi[i] = seq[i];
            }
            if (b>=MAX)
            {
                MAX = b;
                for (int i = 1; i <= n; ++i)
                    ma[i] = seq[i];
            }
        }
        printf("%d %d\n", MIN, MAX);
        for (int j = 1; j <= n; ++j)
            printf("%d ", mi[j]);
        puts("");
         for (int j = 1; j <= n; ++j)
             printf("%d ", ma[j]);
         puts("");
     }
     return 0;
}

规律:逆序时最小,将逆序最大放到末位时最大

n%4==3或0时,最小值为0,其余都为1

举个例子:序列7 6 5 4 3 2 1

此时该序列为最小值,会发现:从7开始每4个数的绝对值为0,每两个数为1,所以若n%4==3时,剩下的最后3个为3 2 1,易得最小值为0,n%4==0、1、2时同理。

最大值MAX(n)=n-MIN(n-1)

AC代码:

 1 #include<cstdio>
 2 #define N 50005
 3 int getMin(int n)
 4 {
 5     if (n % 4 == 3 || n % 4 == 0)
 6         return 0;
 7     return 1;
 8 }
 9 int main()
10 {
11     int n;
12     while (~scanf("%d", &n))
13     {
14         printf("%d %d\n", getMin(n), n - getMin(n - 1));
15         for (int i = n; i > 0; --i)
16         {
17             if (i > 1)printf("%d ", i);
18             else printf("%d\n", i);
19         }
20         for (int i = n-1; i > 0; --i)
21             printf("%d ", i);
22         printf("%d\n", n);
23     }
24     return 0;
25 }
时间: 2024-08-24 18:55:50

ZOJ3798 Abs Problem的相关文章

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

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

组队赛#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.

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.

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

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 thi

浙大月赛ZOJ Monthly, August 2014

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

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall