POJ2805 Inversion水题

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2805 Inversion水题相关的知识,希望对你有一定的参考价值。

Inversion
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4641 Accepted: 2034

Description

The inversion number of an integer sequence a1, a2, . . . , an is the number of pairs (ai, aj) that satisfy i < j and ai > aj . Given n and the inversion number m, your task is to find the smallest permutation of the set { 1, 2, . . . , n }, whose inversion number is exactly m.
A permutation a1, a2, . . . , an is smaller than b1, b2, . . . , bn if and only if there exists an integer k such that aj = bj for 1 <= j < k but ak < bk.

Input

The input consists of several test cases. Each line of the input contains two integers n and m. Both of the integers at the last line of the input is −1, which should not be processed. You may assume that 1 <= n <= 50000 and 0 <= m <= n(n − 1)/2.

Output

For each test case, print a line containing the smallest permutation as described above, separates the numbers by single spaces.

Sample Input

5 9
7 3
-1 -1

Sample Output

4 5 3 2 1
1 2 3 4 7 6 5

Source

Shanghai 2004 Preliminary

问题链接POJ2805 Inversion
问题简述:(略)
问题分析:水题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2805 Inversion */

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n, m, end = 0;
    while (~scanf("%d%d", &n, &m) && (n + m != -2)) {
        int tot = 0;
        for (int i = n; i >= 1; i--) {
            tot += n - i;
            if (tot >= m) {
                end = i;
                break;
            }
        }

        int k = m + end - (n - end) * (n - end - 1) / 2;
        for (int i = 1; i < end; i++) printf("%d ", i);
        printf("%d ", k);
        for (int i = n; i >= end; i--)
            if (i != k) printf("%d ", i);
        printf("\\n");
    }

    return 0;
}

以上是关于POJ2805 Inversion水题的主要内容,如果未能解决你的问题,请参考以下文章

2805=大家快来A水题

POJ 3685 二分套二分(水题

POJ 2545+2591+2247+1338简单水题

POJ 2503Babelfish(水题)stl map存取即可

POJ 2386 Lake Counting (水题,DFS)

POJ 1488 Tex Quotes --- 水题