[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/) ...
随机推荐
- USACO Section 5.3 Big Barn(dp)
USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1 (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...
- poj1084Square Destroyer(LDX解重复覆盖)
题目请戳这里 题目大意:给一个n*n的用单位长度的木棍拼起来的网格图,给每个木棍按图示编号,编号范围1~2*n*(n+1).现在已知图中已经去掉了k个木棍,求还要至少去掉几根木棍能使网格图中不存在正方 ...
- linux(ubuntu) 遇到的问题 --1
1.使用sudo提示用户不在sudoers文件中的解决方法 切换到root用户 su root 查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [root@localhost ~] ...
- Cubieboard 关闭板载led
修改script.bin 找到最后节点[led_para] 修改leds_used = 0 script.bin 一般在系统盘的第一个分区 例如nand就在/dev/nanda sdcard就在/d ...
- Correlation rule tuning
Lots of organizations are deploying SIEM systems either to do their due diligence or because it’s pa ...
- http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.tar.bz2
Index of /archive/qt/4.5 http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.t ...
- STM32菜鸟成长记录---RS485通讯协议的应用
写作原因:近来蛋闲?非也 ! 昨天一同事合作的项目代码出现的bug-----他的上位机每200ms给我发送命令向我这边下位机读取一些数据,在此过程会按下按键做一些另外操作并给他返回数据:(通信是通 ...
- android service和activity的通讯
我们须要用下面方式来启动service: </pre><pre name="code" class="java"><span st ...
- [Swust OJ 412]--医院设置(floyd算法)
题目链接:http://acm.swust.edu.cn/problem/412/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 基于RSA securID的Radius二次验证java实现(PAP验证方式)
基于rsa SecurID的二次验证.RSA server自身可以作为Radius服务器,RSA也可以和其他的软件集合,使用其他的server作为Radius服务器. radius的验证的一般流程如下 ...