[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/) ...
随机推荐
- HTML+CSS笔记 CSS中级 颜色&长度值
颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令颜色 语法: p{co ...
- Node Node
http://www.nodejs.org/ http://outofmemory.cn/code-snippet/1403/node-javascript-classic-introduction- ...
- HDU 1870 愚人节的礼物
题解:简单的数括号问题…… #include <cstdio> char s[1005]; int main(){ while(scanf("%s",s)!=EOF){ ...
- uva 10651 - Pebble Solitaire(记忆化搜索)
题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...
- mahout贝叶斯算法开发思路(拓展篇)1
首先说明一点,此篇blog解决的问题是就下面的数据如何应用mahout中的贝叶斯算法?(这个问题是在上篇(...完结篇)blog最后留的问题,如果想直接使用该工具,可以在mahout贝叶斯算法拓展下载 ...
- UIControl IOS控件编程 及UITextField的讲解
第一部分 UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本字段控件.UIPageCo ...
- c#Winform程序的toolStripButton自己定义背景应用演示样例源代码
C# Winform程序的toolStrip中toolStripButton的背景是蓝色的,怎样改变背景及边框的颜色和样式呢? 实现此功能须要重写toolStripButton的Paint方法 这里仅 ...
- 各浏览器对 window.open() 的窗口特征 sFeatures 参数支持程度存在差异
标准参考 无. 问题描述 使用 window.open 方法可以弹出一个新窗口,其中 open 方法的 sFeatures 参数选项在各浏览器中支持程度不一,这有可能导致同样的代码使各浏览器中弹出窗口 ...
- 「OC」block 和 protocol
一.block (一)简介 block 是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,block 可以在任何时候执行.在多线程.异步任务.集合遍历.集合排序.动 ...
- ubuntu下vpn无反应的解决办法
mv ~/.gconf ~/.gcongbk0 sudo reboot