hdu--1027-next_permutation||dfs

....连跪3把 真无语..

写完这个 看电影去了..

明天就去看 后会无期了 应该不会让人失望的

-------------碎碎念

这题 我一开始自己是用 dfs写的.. 后来看了下discuss 看到个新方法 使用stl中的next_permuntation 速度不仅快了许多 而且代码简洁..

关于 这个的介绍 传送

重点 我把它拿出来

next_permutation函数的原理如下:

在当前序列中,从尾端向前寻找两个相邻元素,前一个记为*i,后一个记为*t,并且满足*i < *t。然后再从尾端

寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第t个元素之后(包括t)的所有元

素颠倒排序,即求出下一个序列了。

    touch   me

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5 int cnt , n , m;
 6 int arr[1010];
 7 bool vis[1010];
 8
 9 void dfs(int pos)
10 {
11     if( cnt==m )
12         return;
13     if( pos==n )
14     {
15         cnt++;
16         if( cnt==m )
17         {
18             for( int i = 0 ; i<n-1 ; i++ )
19             {
20                 cout << arr[i] << " ";
21             }
22             cout << arr[n-1] << endl;
23         }
24     }
25     for( int i = 1 ; i<=n ; i++ )
26     {
27         if( !vis[i] )
28         {
29             arr[pos] = i;
30             vis[i] = true;
31             dfs( pos+1 );
32             vis[i] = false;
33         }
34     }
35 }
36
37 int main()
38 {
39     while( cin >> n >> m )
40     {
41         memset( vis , false , sizeof(vis) );
42         cnt = 0;
43         dfs(0);
44     }
45     return 0;
46 }

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 int arr[1010];
 6 int main()
 7 {
 8     int n , m;
 9     while( cin >> n >> m )
10     {
11         for( int i = 0 ; i<n ; i++ )
12         {
13             arr[i] = i+1;
14         }
15         while(--m)
16         {
17             next_permutation(arr,arr+n);
18         }
19         for( int i = 0 ; i<n ; i++ )
20         {
21             if(i<n-1)
22                 cout << arr[i] << " ";
23             else
24                 cout << arr[i] << endl;
25         }
26     }
27     return 0;
28 }

today:

  在某个阶段,尤其当你寂寞太久的时候,有太多的冲动,把喜欢当成爱,把一秒当成永恒。然而如果不是这么地折腾,你也不会知道自己真的想要的是什么。每个人的青春里都有一条弯路,谁也没法替你走完,但未来总还在。

hdu--1027-next_permutation||dfs

时间: 2024-08-27 10:27:22

hdu--1027-next_permutation||dfs的相关文章

HDU 1027 Ignatius and the Princess II

原题代号:HDU 1027 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1027 原题描述: Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8415    Accepted Submission(s): 4955 Problem

HDU 4921 Map DFS+状态压缩+乘法计数

算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久,其实因为只能取某个链的前缀,所以直接取链长加+1 然后相乘即可,当然因为会出现都是空的那种情况,要去掉,全部乘完之后,要-1 然后就是算权值了,权值等于当前加进来的点的总和 以及 等级相同的点的加成,并不是特别好算,这时候考虑每个状态下的点对全局的贡献,对,就是这个思想,用状态压缩来表示状态,然后这

HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9458    Accepted Submission(s): 5532 Problem Description Now our hero finds the door to the BEelzebub feng5166. He op

hdu 1027 输出第m个全排列(next_permutation)

Sample Input6 4 //输出第4个全排列11 8 Sample Output1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10 1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 using namespace std ; 5 6 int a[2000] ; 7 8 int main () 9 { 10 int n , m ; 11 while (sca

(全排列)Ignatius and the Princess II -- HDU -- 1027

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5822    Accepted Submission(s): 3433 Problem Description Now our h

HDU 1027 Ignatius and the Princess II(求第m个全排列)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10388    Accepted Submission(s): 5978 Problem Description Now our

HDU 1175 连连看(DFS)

Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去.不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的.现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过.玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能

hdu 1501 Zipper dfs

题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.For examp

HDU - 1175 连连看 DFS (记录方向)

连连看HDU - 1175 "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去.不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的.现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过. 玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能

hdu 1455 Sticks——dfs经典的题目

http://acm.hdu.edu.cn/showproblem.php?pid=1455 题意:几根长度的棍子被分成了很多半.问合成多个长度相同的棍子,棍子长度最小是多少. 题解:很明显是dfs.所以我们首先需要找到,这些棍子可能是多长,肯定是最长的棍子的长度到所有棍子长度和之间的某个长度.找到这些可能之后就直接按照这个长度开始搜.想法是搜到和为这个长度之后记录,然后重新再搜,一直到所有棍子都分配自后就完成了. 重要的剪枝:确定每次搜索的起始位置,这个一定是确定的!!!其次就是相同长度的棍子