题意: 给你某个排列 求从下一个排列开始的第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 排列的更多相关文章

  1. poj 1833 排列 STL 全排列公式

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15173   Accepted: 6148 Description 题 ...

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

  3. POJ 1833 生成排列

    题目链接:POJ 1833 /************************************ * author : Grant Yuan * time : 2014/10/19 16:38 ...

  4. poj 1833

    http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...

  5. POJ 1833 排序

    http://poj.org/problem?id=1833 题意: 给出一个排序,求出它之后的第k个排序. 思路: 排序原理: 1.如果全部为逆序时,说明已经全部排完了,此时回到1~n的排序. 2. ...

  6. Paths on a Grid POJ - 1942 排列组合

    题意: 从左下角移动到右上角.每次只能向上或者向右移动一格.问移动的轨迹形成的右半边图形有多少种 题解: 注意,这个图形就根本不会重复,那就是n*m的图形,向上移动n次,向右移动m次. 从左下角移动到 ...

  7. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  8. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  9. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

随机推荐

  1. BestCoder Round #2

    TIANKENG’s restaurant http://acm.hdu.edu.cn/showproblem.php?pid=4883 竟然暴力1.44*10^7  还要*T=100  竟然过了 # ...

  2. 安装numpy/scipy/scikit-learn的方法

    安装numpy 和 scipy sudo yum install lapack lapack-devel blas blas-devel   sudo yum install numpy.x86_64 ...

  3. POJ 3228 Gold Transportation(带权并查集,好题)

    参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...

  4. HDU 3461 Code Lock(并查集,合并区间,思路太难想了啊)

    完全没思路,题目也没看懂,直接参考大牛们的解法. http://www.myexception.cn/program/723825.html 题意是说有N个字母组成的密码锁,如[wersdfj],每一 ...

  5. hdu 4579 Random Walk 概率DP

    思路:由于m非常小,只有5.所以用dp[i]表示从位置i出发到达n的期望步数. 那么dp[n] = 0 dp[i] = sigma(dp[i + j] * p (i , i + j)) + 1 .   ...

  6. Web中的监听器【Listener】

    Servlet监听器:Servlet规范中定义的一种特殊类,它用于监听Web应用程序中的ServletContext.HttpSession和ServletRequest等域对象的创建与销毁事件,以及 ...

  7. Android中自定义Checkbox

    custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...

  8. 转一个distinct用法,很有帮助

    转一个distinct用法,很有帮助 (2011-12-01 15:18:11) 转载▼ 标签: 杂谈 分类: mysql复制 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提 ...

  9. spring中的aware接口

    1.实现了相应的aware接口,这个类就获取了相应的资源. 2.spring中有很多aware接口,包括applicationContextAware接口,和BeanNameAware接口. 实现了这 ...

  10. SqlDBHelper常用方法

    /*============================================================= *.net连接数据库常用方法 *Author : dongny,Li * ...