Ignatius and the Princess II

Ignatius and the Princess II
题意:略
思路:发现一个stl……

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
#define inf 0x3f3f3f
int main()
{
	int n,m;
	while(cin >> n >> m)
	{
		int a[1500];
		for(int i=1; i<=n; i++)
			cin >> a[i];
		m--;
		while(m--)
		{
			prev_permutation(a+1,a+n+1);
		}
		for(int i=1; i<n; i++)
			cout << a[i] << ' ';
		cout << a[n] << endl;
	}
	return 0;
}

next_permutation
这是一个求一个排序的下一个排列的函数。如果要走遍所有的排列,你必须先排序。这是这两个函数使用需要注意的地方。其函数原形为:

template<class BidIt>
bool next_permutation(BidIt first, BidIt last);
template<class BidIt, class Pred>
bool next_permutation(BidIt first, BidIt last, Pred pr);  

next_permutation(a,a+n);//找到紧挨着a的下一个序列;
prev_permutation(a,a+n);//找到紧挨着a的前一个序列;

posted @ 2016-03-16 18:18  zzuli_柚子  阅读(134)  评论(0编辑  收藏  举报