POJ 1833 排列
题意: 给你某个排列 求从下一个排列开始的第k个排列
如果是最后一个排列 则下一个排列为1 2 3 ... n
// 1 用stl 里面的 next_permutation
// 2 用生成下一个排列算法
// 1)从末尾开始找第一个正序 A[i-1]<A[i]
// 2)从i开始找最大的j A[j]>A[i-1]
// 3)交换 A[i-1],A[j]
// 4)将下标从i开始的序列翻转 #include <iostream>
#include <string>
#include<sstream>
#include <cmath>
#include <map>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int s[2000];
int n,k;
void change(int l,int r)
{
while(l<r)
{
swap(s[l],s[r]);
l++;
r--;
}
}
int main()
{
int T;
int i;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&s[i]); while(k--) next_permutation(s,s+n);
for(i=0;i<n-1;i++)
printf("%d ",s[i]);
printf("%d\n",s[i]);
}
return 0;
} /* 2
#include <iostream>
#include <string>
#include<sstream>
#include <cmath>
#include <map>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int s[2000];
int n,k;
void change(int l,int r)
{
while(l<r)
{
swap(s[l],s[r]);
l++;
r--;
}
}
bool permutation()
{
int i=n-1;
while(i>0&&s[i-1]>=s[i]) i--;
if(!i) return false;
int k=i,j=n-1;
for(;j>i;j--)
if(s[j]>s[i-1]){
k=j;
break;
}
swap(s[i-1],s[k]);
change(i,n-1);
return true;
}
int main()
{
int T;
int i;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&s[i]);
while(k--)
{
if(!permutation())
change(0,n-1);
}
for(i=0;i<n-1;i++)
printf("%d ",s[i]);
printf("%d\n",s[i]);
}
return 0;
}
*/
POJ 1833 排列的更多相关文章
- poj 1833 排列 STL 全排列公式
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15173 Accepted: 6148 Description 题 ...
- POJ 1833 排列【STL/next_permutation】
题目描述: 大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 ...
- POJ 1833 生成排列
题目链接:POJ 1833 /************************************ * author : Grant Yuan * time : 2014/10/19 16:38 ...
- poj 1833
http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...
- POJ 1833 排序
http://poj.org/problem?id=1833 题意: 给出一个排序,求出它之后的第k个排序. 思路: 排序原理: 1.如果全部为逆序时,说明已经全部排完了,此时回到1~n的排序. 2. ...
- Paths on a Grid POJ - 1942 排列组合
题意: 从左下角移动到右上角.每次只能向上或者向右移动一格.问移动的轨迹形成的右半边图形有多少种 题解: 注意,这个图形就根本不会重复,那就是n*m的图形,向上移动n次,向右移动m次. 从左下角移动到 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
随机推荐
- 2014 Multi-University Training Contest 6
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uz2v.html Apple Tree http://acm.hdu.edu.cn/showpro ...
- C# textbox 滚动条 随文本输入 滚动
tb_Log.SelectionStart = tb_Log.Text.Length;//设置光标位置 tb_Log.ScrollToCaret();//随文本输入 滚动
- hadoop浅尝 hadoop与hbase交互
在安装好hbase之后,运行一个与hadoop无关的纯hbase程序成功了. 接着写一个hadoop与hbase进行交互的小程序,这个程序的运行方法依然与前文相同, 即导出jar文件在shell下运行 ...
- Ubuntu 12.04下虚拟磁带库mhvtl的安装和使用
项目需要连接一下昆腾虚拟磁带库DXI 6701 ,这玩意太贵,不好得到,先弄个虚拟软件测试了, 网上了一下,有这个软件: mhvtl 主页: https://sites.google.com/ ...
- 源代码管理工具TFS2013安装与使用【转载】
本文来源:博客园:http://www.cnblogs.com/suxinlcq/p/3956930.html 作者:suxin 最近公司新开发一个项目要用微软的TFS2013进行项目的源代码管理,以 ...
- javascript分享到的源码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 初识io流条件状态
一 流状态 C++中的输入输出系统负责记录每一个输入输出操作的结果信息,这些当前的状态信息被包含在io_state类型的对象中.io_state是一个枚举类型(就像open_mode一样),以 ...
- *[topcoder]ChooseTheBestOne
https://www.topcoder.com/stat?c=problem_statement&pm=13146&rd=15852 // Need carefully calc t ...
- Notepad++ 16进制编辑功能
Notepad++可以编辑PE文件(二进制文件即HEX码),2进制.16进制都可以,通过附加的组件HexEditor即可实现. Hex Editor下载 Hex Editor下载页面,包括各个历史版 ...
- NSMutableArray 排序
NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"1",@"3",@"2" ...