描述

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

题目来源

TOJ

从数组的后面开始寻找(前面的数)小于(后面的数)的数字对,将它们交换,然后从(前面的数的位置+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的更多相关文章

  1. TOJ 2130: Permutation Recovery(思维+vector的使用)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130 时间限制(普通/Java): ...

  2. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  5. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 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 ...

  8. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  9. 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 ...

随机推荐

  1. TypedValue.applyDimension的使用

    TypedValue.applyDimension是一个将各种单位的值转换为像素的方法 用法TypedValue.applyDimension(int unit, float value,Displa ...

  2. angular 重定向路由

    const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ...

  3. blog搬家须知

    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻. 地址:这里. 不过这里也是会同步更新的

  4. Mysql的用户基本操作

    创建用户: mysql> create user 'cai'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.0 ...

  5. 【数据结构】单链表&&静态链表详解和代码实例

    喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...

  6. CF914E Palindromes in a Tree

    $ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...

  7. 使用原生实现jquery中的css方法

    由于jquery放在mobile页面上,有时候还是显得有点大,所以今天尝试使用原生来开发,但是习惯了jquery之后,转用原生开发之后,发现原生中,找不到可以替代jquery的css方法,于是对原生的 ...

  8. Maven搭建Spring+SpringMVC+Mybatis+Shiro项目详解

    一. 环境搭建: 1. 开发工具:myeclipse 2014 / IDEA: 2. maven管理版本:apache-maven-3.0+: 3. jdk 1.7.0+4. Tomcat8.0 二: ...

  9. 【Leetcode】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. class Solution { public ...

  10. Android RecyclerView组件和 Spinner(下拉列表框)

    1.RecyclerView <1>知识点介绍 RecyclerView 比 ListView 更高级且更具灵活性. 它是一个用于显示庞大数据集的容器,可通过保持有限数量的视图进行非常有效 ...