题目: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-] = ;
a.push_back(A[len-]);
for(int i=len-; i>=; --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=, fac=, c=;//基数fac从1开始
for(int i=len-; i>=; --i)
ans += (fac*=c++)*cnt[i];
return ans;
}
};

lintcode Permutation Index的更多相关文章

  1. Lintcode: Permutation Index II

    Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...

  2. [OJ] Permutation Index

    LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...

  3. Permutation Index I & II

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  4. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  5. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  6. [LintCode] Permuation Index

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. 《R包的分类介绍》

    R分析空间数据(Spatial Data) R机器学习包(Machine Learning) R多元统计包(Multivariate Statistics) R药物(代谢)动力学数据分析包 R计算计量 ...

  9. R语言︱常用统计方法包+机器学习包(名称、简介)

    一.一些函数包大汇总 转载于:http://www.dataguru.cn/thread-116761-1-1.html 时间上有点过期,下面的资料供大家参考基本的R包已经实现了传统多元统计的很多功能 ...

随机推荐

  1. eclipse常见问题

    使用eclipse进入断点,当弹出"Confir Perspective Switch"视图时,选择"Yes".之后每次进入断点都会自动切换到debug视图. ...

  2. C#_取随机字符

    1.多位数字字母组成,每位取值0-9A-Z /// <summary> /// 获取下一个顺序码根据上一个(数字字母组合) /// </summary> /// <par ...

  3. iOS开发资源(持续更新)

    vm10虚拟机安装Mac OS X10.10教程 马上着手开发 iOS 应用程序 (Start Developing iOS Apps Today) Xcode使用教程详细讲解 (上) Xcode使用 ...

  4. ubuntu14.04 安装 hadoop2.4.0

    转载:ubuntu搭建hadoop-Ver2.6.0完全分布式环境笔记 自己在搭建hadoop平台时,碰到一些困难,按照该博文解决了问题,转载一下,作为记录. 2 先决条件 确保在你集群中的每个节点上 ...

  5. webform 分页、组合查询综合使用

    界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...

  6. jquery ui 中的插件开发

    1  $.widget() 必须引用 <script src=")" type="text/javascript"></script> ...

  7. Java 中类型转换

    int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf( ...

  8. linux时间同步ntp服务的安装与配置

    1.首先安装NTP [root@localhost /]# yum install ntp -y 2.修改NTP配置文件,添加NTP服务器的网络位置    /etc/ntp.conf # For mo ...

  9. windows下C++高精度计时

    写代码时,经常会计算某一段代码的运行时间,以下提供一个微秒级别的类供参考 class CTimeCost { public: CTimeCost(const string &str) : m_ ...

  10. java学习之面向对象(3)

    下面来谈谈java编程中的一些语法: 1.什么是对象数组? 对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋值给数组. 对象数组的声明: 类名[]  对象数组名称  = n ...