TOJ 4003 Next Permutation
描述
It is an interesting exercise to write a program to print out all permutations of 1, 2, …, n. However, since there are 6227020800 permutations of 1, 2, …, 13, it is unlikely that we would ever run this program on an input of size more than 10.
However, here is another interesting problem whose solution can also be used to generate permutations. We can order the permutations of 1, 2, …, n under the lexicographic (or dictionary) order. Here are the permutations of 1,2,3 in lexicographic order:
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
The problem we have is the following: given a permutation of 1,2, …, n,
generate the next permutation in lexicographic order. For example, for 2
3 1 4 the answer is 2 3 4 1.
输入
The first line of the input contains two integers, N and K. This is
followed by K lines, each of which contains one permutation of 1, 2,…,N.
You may assume that 1 ≤ N ≤ 1000 and K ≤ 10.
输出
The output should consist of K lines. Line i should contain the
lexicographically next permutation correponding to the permutation on
line i+1 in the input.
样例输入
3 2
3 1 2
2 3 1
样例输出
3 2 1
3 1 2
题目来源
从数组的后面开始寻找(前面的数)小于(后面的数)的数字对,将它们交换,然后从(前面的数的位置+1)开始将到N-1的数从小到大排序。
寻找: (前面的数的位置i)从N-2到0,(后面的数的位置j)从N-1到i+1
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int N,K;
int a[];
while( scanf("%d %d",&N ,&K)!=EOF ){
while(K--){
for(int i=; i<N; i++){
scanf("%d",&a[i]);
}
int flag=;
for(int i=N-; i>= && !flag; i--){
for(int j=N-; j>=i+; j--){
if(a[i]<a[j]){
int t=a[j];
a[j]=a[i];
a[i]=t;
sort(a+i+,a+N);
flag=;
break;
}
}
}
flag=;
for(int i=; i<N; i++){
if(flag)printf(" ");
printf("%d",a[i]);
flag=;
}
printf("\n");
}
}
return ;
}
TOJ 4003 Next Permutation的更多相关文章
- TOJ 2130: Permutation Recovery(思维+vector的使用)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130 时间限制(普通/Java): ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
随机推荐
- 数据库索引与b+树
数据库索引详解 索引 当我们在设计数据库的时候,对表的一些属性有时会加上索引,但索引为什么能提高检索速率呢?是不是用了索引就一定可以提高效率呢?不同索引之间有什么区别呢?搞懂这些问题是灵活运用索引的必 ...
- Time - Time-interval Measurements
public class TimeHelper { private long _start, _stop, _elapsed; /// <summary> /// 获取初始时间戳 /// ...
- go-spew golang最强大的调试助手,没有之一
go内置的fmt.sprintf已经很强大了,但是和spew比起来还是相形见绌,这里来一个例子. import ( "fmt" "github.com/davecgh/g ...
- Stars in Your Window(线段树求最大矩形交)
题目连接 http://poj.org/problem?id=2482 Description Fleeting time does not blur my memory of you. Can it ...
- 十二、Nodejs 包与 NPM 第三方模块安装 package.json 以及 CNPM
1. 包 Nodejs 中除了它自己提供的核心模块外,我们可以自定义模块,也可以使用第三方的模块.Nodejs 中第三方模块由包组成,可以通过包来对一组具有相互依赖关系的模块进行统一管理. 在 Nod ...
- 有符号数和无符号数------c++程序设计原理与实践(进阶篇)
有符号数与无符号数的程序设计原则: 当需要表示数值时,使用有符号数(如 int). 当需要表示位集合时,使用无符号数(如unsigned int). 有符号数和无符号数混合运算有可能会带来灾难性的后果 ...
- 1235: 入学考试[DP]
1235: 入学考试 [DP] 时间限制: 1 Sec 内存限制: 128 MB 提交: 37 解决: 12 统计 题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近 ...
- uoj #298. 【CTSC2017】网络
#298. [CTSC2017]网络 一个一般的网络系统可以被描述成一张无向连通图.图上的每个节点为一个服务器,连接服务器与服务器的数据线则看作图上的一条边,边权为该数据线的长度.两个服务器之间的通讯 ...
- 安装jdk环境
1.jdk安装包准备: 下载链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- DEM反应不收敛问题
之前算DEM反应一直不收敛,后来把计算规模减少到两个固定颗粒,也就是仿照reactive-chemistry和evaperation这两个算例. 目前reactive-chemistry这个算例,把各 ...