POJ 2085 Inversion

 

Inversion
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2631   Accepted: 1143

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

 

 

/*
找了好久規律,暈了好多次,改了又寫,寫了又改,終於找出來了,這個問題主要基於以下考慮:
對於任意一個序列i, i + 1, ..., j其最大的inversion number是全部逆序的情況,即
j, j - 1, ..., i + 1, i,值記爲in(i, j) = (j - i + 1) * (j - i) / 2
所以這個問題的解決的步驟如下:
(1)對於輸入n, seq, 從後往前考慮找到可以涵蓋seq值的i, 即in(i, n) >= seq
(2)由(1)可知,i -> n足夠用來表示值爲seq的insersion number,所以1-> i - 1只要按照
升序打印即可
(3)剩下的i -> n如何表示值爲seq的逆序數呢?考察幾個例子不難發現.剩下的i -> n的形式
一定是 k, v1, v2, ..., vn-1,其中k爲i->n中的任意一個數, {v1, v2, v3, ..., vn-1}是
除k以外剩下的數的完全逆序形式.例如:加入i = 5, n = 10, {7, 10, 9, 8, 6, 5}就是這樣
一種形式.那麼剩下的任務就是找出這個k即可.由上述分析我們不難列出方程式:
k - i + (n - i) * (n - i - 1) / 2 = seq, 其中k - i表示大頭的k對這個數列逆序的貢獻
數, (n - i) * (n - i - 1) / 2表示剩下的除k以外的i->n完全逆序數列的貢獻度,這樣把k
解出來即可.
*/
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章