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 ...
随机推荐
- duilib入门简明教程 -- 前言(1)
关于duilib的介绍就不多讲了,一来不熟,二来小伙伴们想必已经对比了多个界面库,也无需赘述.下面进入正题: 不看广告看疗效! 已有众多知名公司采用duilib做为界面库,如华为网盘. ...
- Ubuntu下添加Python环境变量
最近把自己在windows上的Django项目放在的Ubuntu上,启动时提示找不到模块 原来是自己的项目用的是虚拟环境,安装的第三方库全部在虚拟环境中 AutoTestSite/venv/Lib/s ...
- mvc - view传值到js
http://www.cnblogs.com/akwwl/p/5238975.html
- 重拾C,一天一点点_7
标准库,atof()函数包含在头文件<stdlib.h>中 /******把字符串s转换为相应的双精度浮点数*******/ #include <stdio.h> #inclu ...
- linux下利用httpd搭建tomcat集群,实现负载均衡
公司使用运营管理平台是单点tomcat,使用量大,或者导出较大的运营数据时,会造成平台不可用,现在需要搭建tomcat集群,调研后,决定使用apache的httpd来搭建tomcat集群.以下是搭建步 ...
- 《C#多线程编程实战》1.11 Monitor.TryEnter()避免死锁
这章的内容是真的有意思 特别是代码. 先贴上代码: class Program { static void Main(string[] args) { object lock1 = new objec ...
- 「CF622F」The Sum of the k-th Powers「拉格朗日插值」
题意 求\(\sum_{i=1}^n i^k\),\(n \leq 10^9,k \leq 10^6\) 题解 观察可得答案是一个\(k+1\)次多项式,我们找\(k+2\)个值带进去然后拉格朗日插值 ...
- 1232: 买不到的数目 [DP、数学]
1232: 买不到的数目 [DP.数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 21 解决: 10 统计 题目描述 小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的 ...
- jquery常用事件——幕布
jquery常用事件:https://mubu.com/doc/yIEfCgCxy0
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...