[Swust OJ 541]--排列字典序问题
题目链接:http://acm.swust.edu.cn/problem/0541/
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 }的一个排列,计算出这个排列的字典序值,以及按字典序排列的下一个排列。
第1 行是元素个数n(n < 15)。接下来的1 行是n个元素{1,2,..., n }的一个排列。
第一行是字典序值,第2行是按字典序排列的下一个排列。
|
1
2
|
8
2 6 4 5 8 1 7 3
|
|
1
2
|
8227
2 6 4 5 8 3 1 7
|
比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小的数没有;
#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*************************/
#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]--排列字典序问题的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 715]--字典序问题(组合数预处理/数位dp)
题目链接:http://acm.swust.edu.cn/problem/715/ Time limit(ms): 1000 Memory limit(kb): 65535 在数据加密和数据压缩中 ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 795]--Penney Game
题目链接:http://acm.swust.edu.cn/problem/795/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- [Swust OJ 491]--分数的位置(简单版)
题目链接:http://acm.swust.edu.cn/problem/0491/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...
随机推荐
- 我用过的linux命令--安装Hadoop
1. hadoop软件传送给虚拟机 还是利用WinSCP把hadoop软件安装包,放到linux的Downloads文件夹中. 2. 选择安装目录 把hadoop安装包copy到这个安装目录中,这里我 ...
- 电脑bios到底是什么?
没有哪个玩电脑的人不知道电脑bios,但是真正能明白bios是什么的?身边却没几个,甚至大多数电脑维修站的人员对bios也不够详细了解.一般人不去关心bios是因为它离我们的电脑真正使用仍有一段距离. ...
- Apache RewriteCond RewriteRule 入门和Laravel去掉index.php
Ci删除index.php办法: 创建.htaccess 文件放到网站的根目录下,文件中的内容如下: RewriteEngine onRewriteCond %{REQUEST_FILENAM ...
- 基于Visual C++2013拆解世界五百强面试题--题16-进制分析
清写出下列代码的输出内容 #include <stdio.h> int main() { int a = -1, b = -12, c = -123, d = -1234; printf( ...
- js 获取前天、昨天、今天、明天、后天的时间
js 获取前天.昨天.今天.明天.后天的时间 2011-05-19 21:03 <html><head><meta http-equiv="Content- ...
- Eclipse快捷键 今天又学会了几个不常用的 收藏了
1.Ctrl+e 打开所有已经打开的文件列表,当你使用Eclipse打开了N多文件的时候,需要找到一个你之前打开过 的文件,是不是就很费 ...
- 深入分析MySQL ERROR 1045 (28000)
这几天在MySQL新建用户后.出现訪问拒绝的问题,错误码为ERROR 1045(28000).在网上搜索了非常久.找到了非常多解决的方法,但非常遗憾的是这么多办法没有一个能解决该问题.尽管出现的错误码 ...
- 《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定
1.TCP的服务 在一个TCP连接中.仅有双方进行彼此通信. TCP通过下列方式来提供可靠性: 1)应用数据被切割成TCP觉得最适合发送的数据块. 这和UDP全然不同,应用程序产生的数据报长度保持不变 ...
- 【转】Ubuntu下deb包的安装方法
[转]Ubuntu下deb包的安装方法 deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian P ...
- [译]MDX 介绍
关于MDX MDX (Multi Dimensional eXpression language) 是非常强大的工具,可以将你的多维数据库/cube 发挥到极致. 本文会覆盖MDX基础,并且希望能使你 ...