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 ...
随机推荐
- 使用Recyclerview实现图片水平自动循环滚动
简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...
- 将“100px” 转换为100
parseInt("100px") //结果是100
- 随便写个bat存档
@echo off @COLOR @echo ------------切换Hosts环境--------------- :Again @set /p choice="切换模式:A:应用环境, ...
- owinAuthorize
Nuget包获取 Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.1.2 Install-Package Microsoft.Owin. ...
- 认识学习MVC这家伙
通过最近的学习认识,只能感慨这玩意太强大了! 以前看了一些MVC的入门教程,看入门教程我感觉不能去体会它的强大,可以看看它的扩展点,通过扩展点去真正的试着了解它,体会它的强大. 它的验证.模型绑定提供 ...
- 十三、Node.js-fs模块(上)
Node.js内置的fs模块就是文件系统模块,负责读写文件以及对文件进行相关操作. 下面直接可参考下面的代码进行fs模块里面基本方法的学习: /** * Created by Administrato ...
- kubernetes dashboard 安装
环境:CentOS Linux release 7.3.1611 (Core)IP:192.168.0.103 [1]组件安装yum install device-mapperyum install ...
- Delphi XE8如何同Eclipse使用相同的Android SDK?
我的Android SDK是单独安装的:Eclipse也是最新版的,并不是谷歌提供的集成了SDK的那个Eclipse:Delphi XE8安装后,我并没有通过XE8里面下载Android SDK到XE ...
- EF进阶篇(二)——CRUD
前言 不写前言了... 内容 我们都知道我们修改的实体状态添加到上下文里面,然后上下文根据实体状态生成相应的SQL执行脚本,去隐式执行到数据库中.(屏幕前的你暂且这样理解吧,因为小编语文没有学好,太通 ...
- 使用python-docx生成Word文档
首先是安装python-docx:(centos环境) pip install python-docx 基本方法使用: from docx import Document from docx.shar ...