Written by razrlele
14:00 October 12, 2014
Inversion
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4004 | Accepted: 1790 |
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.
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
1 2 3 |
5 9 7 3 -1 -1 |
Sample Output
1 2 |
4 5 3 2 1 1 2 3 4 7 6 5 |
Source
因为题目要求的是符合条件的最小字典排序字符串,所以可以证明答案字符串必须是由递增数列加一个+最大元素+递减数列组成.
比如说:
6 5 7 4 3 2 1 的倒序数就等于 5 7 6 4 3 2 1, 但是根据题意:
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
故前者是大于后者的,所以答案只能是后者.
然后就可以使用贪心算法来求了,给的倒序数是m,首先把n个数逆序排列, 倒序数是n*(n-1)/2,如果把逆序第i个数移到数列列首的话数列的逆序数就会减小(n-i),之前又可知答案数列中最大数之前必须是递增数列,只需要从逆序数列最后一个数开始贪心即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/************************************************************************* > File Name: poj2085.cpp > Author: razrLeLe > Mail: razrlele@outlook.com > Homepage: https://yueyu.io > Created Time: 2014年10月12日 星期日 10时49分48秒 ************************************************************************/ #define LOCAL #include <vector> #include <list> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <string.h> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; const int MAX = 50010; bool used[MAX]; ll n, m; ll i, j, k; int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif while(1) { cin >> n >> m; if(n == -1 && m == -1) break; memset(used, false, sizeof(used)); i = 1; m = n*(n-1)/2 - m; if(m <= (n-1)) i = n-m; while(m != 0) { int tmp = (n-i); if(tmp <= m) { used[i] = true; m -= tmp; } i++; } for(j = 1; j <= n; j++) if(used[j]) cout << j << " "; for(j = n; j >= 1; j--) if(!used[j]) cout << j << " "; printf("\n"); } return 0; } |