[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/) ...
随机推荐
- Springmvc异步上传文件
<script src="js/jquery.js" type="text/javascript"></script><scrip ...
- System.Reflection.Emit学习
C#反射发出System.Reflection.Emit学习 分享: 1 一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于Syste ...
- Protel 99SE PCB 打印技巧
1. 打开 Protel99SE PCB 设计文档.从菜单File 下单击Print/Preview 打印预览菜单.出现PCB 打印预览介面. 2.从File 下单击 Setup Printer 设置 ...
- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
2016-07-18 16:08:20 [main:53] - [WARN] Exception encountered during context initialization - cancell ...
- Pascal向C++的跨越
最近从pas转向了C++,觉得需要在语言上总结对比一下,以及记录一些注意点,关于STL,还需要之后好好地学习.同时,希望这篇文章对从pas转C++的同学有所帮助. 基本类型 首先是基本类型的比较: P ...
- [Python]Unicode转ascii码的一个好方法
写这篇文章的是一位外国人,他遇到了什么问题呢?比如有一个 Unicode 字符串他需要转为 ascii码: >>> title = u"Klüft skräms inför ...
- hdu5115 Dire Wolf【区间dp】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4361169.html ---by 墨染之樱花 [题目链接]http://acm.hdu.e ...
- Oracle行转列实例
行转列之前的数据: select * from tbname; 行转列之后的数据: select pud, listagg(ud, ',') within group(order by null) a ...
- 【转】Nginx+Tomcat搭建高性能负载均衡集群
最近对负载均衡比较感兴趣,研究了公司的负载均衡的配置,用的是阿里的SLB,相当于不用运维,只需要在后台进行简单的配置就能完成Tomcat的负载均衡,索性在网上找了几篇文章去尝试搭建一个集群,然而很多都 ...
- [Swust OJ 799]--Superprime Rib(DFS)
题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...