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 ...
随机推荐
- 【C#】is 和 as
看个例子: public class User { } public class Group { } class Program { static void Main(string[] args) { ...
- C++初始化,之不明白篇 cout<<x<<endl 与 cout<<"x = "<<cout<<x<<endl的输出的值会不一样
代码如下 #include <iostream> using namespace std; class point { public : int x; int y; ...
- IEnumerable与IQueryable区别
最近在使用MongoDB的时候,发现查询很慢,一个根据Id查询的语句竟然用了50秒,debug了一下,没发现什么大问题,但是另一个查询的语句只用了2秒,对比了一下,发现50s的那个语句使用的IEnum ...
- Win7 WPF程序无法接受外部拖拽
最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...
- git Problem with the SSL CA cert (path? access rights?)
问题: [root@localhost opt]# git clone https://github.com/docker/docker.git 正克隆到 'docker'...fatal: unab ...
- Java以邮件附件的方式发送excel文件
String to = "xxx@qq.com"; // 收件人的QQ邮箱 String from = "xxx@qq.com"; // 发件人的QQ邮箱 St ...
- 用Python写一个随机密码生成器
# /bin/python3 import sys import time import random strs = [ i for i in range(32,128) ] #产生密码的ASCII码 ...
- 八大排序算法的python实现(二)希尔排序
代码: #coding:utf-8 #author:徐卜灵 # 希尔排序的实质就是分组插入排序,该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. # 希尔排序,也称递减增量排序算法, ...
- Unity---动画系统学习(3)---使用状态机来实现走、跑、转弯等的动画切换
1. 初始设置 用动画学习笔记(2)中方法,把动画全都切割好. 拖进状态机并设置箭头.并设置具体箭头触发的事件. 在状态机左侧中添加参数,Float和Int类型参数只能从-1~1之间变化 Float: ...
- webpack(二)解析es6并打包
一.前言 ECMAScript 6(ES6)的发展速度非常之快,但现代浏览器对ES6新特性支持度不高,所以要想在浏览器中直接使用ES6的新特性就得借助别的工具来实现.Babel是一个广泛使用的转码器, ...