跳到正文

题解

题解:P1088 [NOIP 2004 普及组] 火星人

int_inf约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年3月2日
收录
2026年10月1日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
14
题目
P1088

版本

  1. v1当前

题目传送门 & AC记录

Pre

前置问题:P1706 全排列问题

Solution

这道题的核心题意读下来,其实就是让我们去枚举第 mm 个全排列。对于部分不想写 DFS 的同学来说,算法头文件中的 next_permutation() 就是一个不错的选择。

next_permutation() 函数的原型为

C++
bool next_permautation(iterator start, iterator end);

怎么样,是不是有点眼熟?没错,它和同样属于 <algorithm> 库的 sort 函数十分相似。其中 start 是指起始位置, end 是指结束位置。有了这个挂,我们便可以轻松写出以下代码。

Code

C++
#include<bits/stdc++.h>
using namespace std;
int a[10001],n,m;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	while(m--)
	{
		next_permutation(a+1,a+n+1);//枚举到第 m 个全排列 
	}
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<" ";//输出第 m 个全排列 
	}
    return 0;//下次再见 
}

事已至此,不如留下赞和关注,后会有期。

原文
存档评论4 条抓取于