lintcode Permutation Index

 题目:http://www.lintcode.com/zh-cn/problem/permutation-index/

排列序号

给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从1开始。

样例

例如,排列[1,2,4]是第1个排列。

思路:

1.直接暴力,利用c++中<algorithm>中的next_permutation()方法不断的寻找下一个全排列,直到相等为止!

2.首先观察一个全排列, 例如:95412 = X

  a.题目转换成按照字典序,这个全排列之前有多少个全排列。

  b.X的前面的所有全排列中,对于位置1上可以是5, 4, 1, 2任意一个数,而且对应的全排列的基数都是4!个。

  c.同理位置2, 3, 4, 5对应的基数分别是,3!,2!,1!,0!(0!==0)。

  d.得到该位置对应的基数后,那么该位置对应多少个可变数字?9所在位置对应的可变数字的个数为4,分别是5,4,1,2;

   5所在位置对应的可变数字是4,1,2;4所在位置对应的可变数字是1,2,;1所在位置的对应的可变数字:无。2所在位置

     对应可变数也是无。

  e.可以得到结论,X全排列某个位置上对应的可变数字的个数 == 这个数后面有多少个比它小的数的个数。

  f.为了得到某个数后面有多少个比它小的数的个数,我们采用折半插入排序(从后向前插入)。

class Solution {
public:
    /**
     * @param A an integer array
     * @return a long integer
     */
    long long permutationIndex(vector<int>& A) {
        // Write your code here

        //阿欧,知道会超时,试一试还真tm超时
        // vector<int> permu(A.begin(), A.end());
        // sort(permu.begin(), permu.end());
        // int cnt = 0;
        // do{
        //     int i;
        //     for(i=0; i<A.size(); ++i)
        //         if(A[i]!=permu[i])
        //             break;
        //     ++cnt;
        //     if(i>=A.size()) break;
        // }while(next_permutation(permu.begin(), permu.end()));
        // return cnt;

        vector<int> a;
        int len = A.size();
        int cnt[len];
        cnt[len-1] = 0;
        a.push_back(A[len-1]);
        for(int i=len-2; i>=0; --i){//统计每个数后面有多少个比它小的数的个数
            vector<int>::iterator it = lower_bound(a.begin(), a.end(), A[i]);
            cnt[i] = it-a.begin();
            a.insert(it, A[i]);
        }

        long long ans=1, fac=1, c=1;//基数fac从1开始
        for(int i=len-2; i>=0; --i)
            ans += (fac*=c++)*cnt[i];
        return ans;
    }
};

 

时间: 2024-09-08 16:33:07

lintcode Permutation Index的相关文章

R语言中的机器学习包

Machine Learning & Statistical Learning (机器学习 & 统计学习) 网址:http://cran.r-project.org/web/views/MachineLearning.html维护人员:Torsten Hothorn 版本:2008-02-18 18:19:21 翻译:R-fox, 2008-03-18  机器学习是计算机科学和统计学的边缘交叉领域,R关于机器学习的包主要包括以下几个方面: 1)神经网络(Neural Networks): 

LeetCode:Permutation Sequence

题目链接:http://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132&q

UVa 11129:An antiarithmetic permutation

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2070 [原题] A permutation of n+1 is a bijective function of the initial n+1 natural numbers: 0, 1, ... n. A permutation p is cal

UVa 10252 Common Permutation (water ver.)

10252 - Common Permutation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1193 Given two strings of lowercase letters, a and b, print the longest string

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically

递归-Lintcode name &amp;amp;#39;Solution&amp;amp;#39; is not defined EXITCODE=1

问题描述 Lintcode name 'Solution' is not defined EXITCODE=1 题目是写一个用递归找到从1到N位的最大数字 比如N=2 返回[12....99].下面是我写的代码,逻辑结果我认为是正确的,但是提交时返回: File ""Main.py"" line 7 in solution = Solution() NameError: name 'Solution' is not defined EXITCODE=1.这跟我的程序

c++-实现strStr函数的一个C++程序lintcode compile不能完全通过

问题描述 实现strStr函数的一个C++程序lintcode compile不能完全通过 class Solution {public: /** * Returns a index to the first occurrence of target in source * or -1 if target is not part of source. * @param source string to be scanned. * @param target string containing t

[LeetCode]--31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3