Noip2004 火星人 【STL之next_permutation】

本人水平有限,题解不到为处,请多多谅解

本蒟蒻谢谢大家观看

题目传送门

直接用全排列即可。

推荐使用next_pumutation

next_permutation(start,end),和   prev_permutation(start,end)。

这两个函数作用是一样的,

区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列

推荐博客:传送门

code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3  
 4 int a[10001];
 5 int n,m;
 6 inline int read()
 7 {
 8     int x=0,f=1;
 9     char ch=getchar();
10     while(ch<'0'||ch>'9')
11     {
12         if(ch=='-')
13         f=-1;
14         ch=getchar();
15     }
16     while(ch<='9'&&ch>='0')
17     {
18         x=(x<<1)+(x<<3)+(ch^48);
19         ch=getchar();
20     }
21     return x*f;
22 }
23 int main()
24 {
25     n=read();
26 //  printf("\n");
27     m=read();
28     for (int i=1;i<=n;i++)
29     {
30         a[i]=read();
31     }
32      for(int i=1;i<=m;i++)
33     {
34         next_permutation(a+1,a+n+1);
35     }
36     for (int i=1;i<n;i++)
37     {
38         printf("%d ",a[i]);
39     }
40     printf("%d",a[n]);
41     return 0;
42 }

猜你喜欢

转载自www.cnblogs.com/nlyzl/p/11830418.html