题目链接:http://acm.swust.edu.cn/problem/0541/

Time limit(ms): 2000      Memory limit(kb): 65535

n个元素{1,2,..., n }有n!个不同的排列。将这n!个排列按字典序排列,并编号为0,1,…,n!-1。每个排列的编号为其字典序值。例如,当n=3时,6 个不同排列的字典序值如下:

0       1     2    3     4    5

123 132 213 231 312 321

任务:给定n 以及n 个元素{1,2,..., n }的一个排列,计算出这个排列的字典序值,以及按字典序排列的下一个排列。

Description

第1 行是元素个数n(n < 15)。接下来的1 行是n个元素{1,2,..., n }的一个排列。

Input

第一行是字典序值,第2行是按字典序排列的下一个排列。

Output
1
2
8
2 6 4 5 8 1 7 3
Sample Input
1
2
8227
2 6 4 5 8 3 1 7
Sample Output
 
 
解题思路:两部曲(咳咳~~)
一题目例子为例

比2小的数有1个,则 tot+=1*7!;

比6小的数有4个,则 tot+=4*6!;

比4小的数有2个,则 tot+=2*5!;

比5小的数有2个,则 tot+=2*4!;

比8小的数有3个,则 tot+=3*3!;

比1小的数有0个,则 tot+=0*2!;

比7小的数有1个,则 tot+=1*1!;

比3小的数没有;

在实际求解中可以通过减去前面比当前数小的数来确定后面当前数小的数(方便程序的编写)
 
对于求下一个序列,冲最后找,找到一个比最后一个数小的数把最后一个数放在之前就可以了
当然懒人嘛(比如说我)就next_permutation()你值得拥有~~~
 
代码如下
 #include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, i, j, k, t, *p, x[];
x[] = ;
for (i = ; i < ; i++)
x[i] = x[i - ] * i;
while (cin >> n)
{
k = ;
p = new int[n + ];
for (i = ; i < n; i++)
cin >> p[i];
for (i = ; i < n - ; i++)
{
t = p[i] - ;
for (j = ; j < i; j++)
if (p[j] < p[i])
t--;
k += t*x[n - i - ];
}
cout << k << endl;
next_permutation(p, p + n);
for (i = ; i < n; i++)
cout << p[i] << ' ';
cout << endl;
}
return ;
}
 用数位dp还是超时了(其实就这个状态设计本身来说不超时才是怪事,不想多说),代码先放在这里
 /*******************数位dp*************************/
#include <iostream>
#include <algorithm>
using namespace std; int n, bit[], vis[], flag;
char s[]; int dfs(int pos, bool limit){
if (pos >= n) return ;
int last = limit ? bit[pos] : n;
int ret = ;
for (int i = ; i <= last; i++){
if (vis[i]) continue;
vis[i] = ;
ret += dfs(pos + , limit&&i == last);
vis[i] = ;
}
return ret;
} int main(){
cin >> n;
for (int i = ; i < n; i++) cin >> bit[i];
cout << dfs(, ) - << endl;
next_permutation(bit, bit + n);
for (int i = ; i < n; i++)
cout << bit[i] << ' ';
cout << endl;
return ;
}

[Swust OJ 541]--排列字典序问题的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 715]--字典序问题(组合数预处理/数位dp)

    题目链接:http://acm.swust.edu.cn/problem/715/ Time limit(ms): 1000 Memory limit(kb): 65535   在数据加密和数据压缩中 ...

  5. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  6. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  7. [Swust OJ 795]--Penney Game

    题目链接:http://acm.swust.edu.cn/problem/795/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  8. [Swust OJ 491]--分数的位置(简单版)

    题目链接:http://acm.swust.edu.cn/problem/0491/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

    题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...

随机推荐

  1. JS数组方法总结

    数组的常用方法总结   不改变原数组 1.Array.length;                       //获取数组长度 2.Array.join();                   ...

  2. A Byte of Python 笔记(7)数据结构:列表、元组、字典,序列

    第9章 数据结构 数据结构,即可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python 有三种内建的数据结构--列表.元组和字典. list = ['item1', 'item2' ...

  3. Latex调整行距

    修改行间距的方法: \usepackage{setspace}%使用间距宏包 \begin{document} \begin{spacing}{2.0}%%行间距变为double-space 双倍行距 ...

  4. android screenOrientation

    Activity: android:screenOrientation 横(landscape)竖(portrait)屏显示. 如果想让它一直是横屏显示的话,xml:android:screenOri ...

  5. 高性能Mysql主从架构的复制原理及配置详解(转)

    温习<高性能MySQL>的复制篇. 1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台 ...

  6. 代码收藏 JS实现页内查找定位功能

    前部分为IE下搜索方法 用TextRange来实现 后部分为firefox.chrome下搜索方法 var nextIndex = 0; var searchValue = ''; var input ...

  7. 双网卡绑定(suse)

    网卡绑定技术有助于保证高可用性特性并提供其它优势以提高网络性能,Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个聚合起来的设备看起来是一个单独的以太网接口设备,就是两块网卡具有相同的IP ...

  8. android AChartEngine源代码

    昨天翻自己曾经下过的apache开源project项目,看到一个AChartEnginee看了一下自带的Demo才意识到这个东西的强大.立刻想把源代码down一份,在CSDN上有人挂5分让人下载,实在 ...

  9. Linux通过网卡驱动程序和版本号的信息

    检查卡制造商和信号 查看基本信息:lspci 查看详情:lspci -vvv   # 3小作文v 查看卡信息:lspci | grep Ethernet 查看网卡驱动 查看网卡驱动信息:lspci - ...

  10. Ext JS学习第十三天 Ext基础之 Ext.Element

    •Ext.Element提供了181个方法,嗯,还没完,只是在4.1版本中是这样,最新的4.2版本貌似又增加了新方法,可谓是相当丰富给力.那么根据操作类型基本可以分为查询系.DOM操作系.样式操作系. ...