解法参考了:http://blog.csdn.net/lanxu_yy/article/details/17261527思路:思路1是用NP的方式来罗列出所有的排列再找出第k个结果,这种方法的时间复杂度与空间复杂度比较高。思路2是研究排序结果的规律,例如取n是,结果可以分为n个组,第一组是第一个数字...
分类:
其他好文 时间:
2015-05-28 22:45:30
阅读次数:
226
题意:
从1-n的数,让你选择一些数来构造,要求每个相邻的数之间的绝对值之差有k种
题解:
先放好一个1 然后往后面插数字 先满足绝对值不同的 然后全插绝对值为1的,
代码:
#include
#include
int main()
{
int n, k, a[100005], mark[100005];
while(scanf("%d %d", &...
分类:
其他好文 时间:
2015-05-27 13:55:47
阅读次数:
139
下面摘抄的别人的讲解非常清楚最近刷leetcode的时候遇见next permutation这道题,感觉挺有意思的一个题目,递归的方法是较简单并且容易想到的,在网上搜了其余的解法,就是std::next_permutation非递归解法,但是让人不是很舒服的就是关于原理的部分,千篇一律的都是摘抄.....
分类:
其他好文 时间:
2015-05-24 21:50:52
阅读次数:
890
题意:
给出一个串,要求按照字典序输出所有排列。分析:
直接利用STL 里的next_permutation()就好,重新定义一个cmp函数,没有把cmp放进next_permutation(),我都WA哭了。。。#include
#include
#include
#include
#include ...
分类:
其他好文 时间:
2015-05-24 13:00:31
阅读次数:
143
题意:给你一个序列 , 给你一个mark 矩阵 , 如果mark[i][j] = 1, 则代表序列i j 可以交换,需要求出交换之后字典序最小的序列
题解:
floyd 处理一遍,然后靠前的优先选择最小的数 , 然后没了
代码:
#include
#include
#define N_node 305
int n, dis[N_node][N_node], va...
分类:
其他好文 时间:
2015-05-24 10:12:05
阅读次数:
133
找规律:题解:本文讲解转自Code Ganker稍稍修改“http://blog.csdn.net/linhuanmars/article/details/20434115”“这道题是给定一个数组和一个排列,求下一个排列。算法上其实没有什么特别的地方,主要的问题是经常不是一见到这个题就能马上理清思路...
分类:
其他好文 时间:
2015-05-22 16:47:48
阅读次数:
205
1 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.Given nn and kk, return the kthk^{th} permutation sequence.
使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制。下...
分类:
编程语言 时间:
2015-05-22 13:36:24
阅读次数:
129
题目大意:给定nn,要求构造三个00~n?1n-1的排列A,B,CA,B,C,使得对于任意i(i∈[0,n?1])i(i\in[0,n-1])满足Ai+Bi≡Ci(mod n)A_i+B_i≡C_i(mod\ n)
首先我们来考虑nn是奇数的情况。以n=7n=7为例
A 0 1 2 3 4 5 6A\ 0\ 1\ 2\ 3\ 4\ 5\ 6
B 6 4 2 0 5 3 1B\ 6\ 4\ 2\...
分类:
其他好文 时间:
2015-05-18 14:48:07
阅读次数:
208
Problem:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the l...
分类:
编程语言 时间:
2015-05-14 16:38:48
阅读次数:
146
1 Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the...
分类:
编程语言 时间:
2015-05-14 12:08:12
阅读次数:
154