题意:
Tom学会了通过写程序求出一个1-n的排列的逆序对数,但他的老师给了他一个难题:
给出一个1-n的排列,求所有字典序比它小的1-n的排列的逆序对数之和。
Tom一时不知道该怎么做,所以他来找你帮他解决这个问题。
因为数可能很大,答案对109+7取模。
从前往后推,先计算1-k的所有排列可以产生逆序总数,
先假设db[2]为1-2的结果,那么我们来看3的排列,他是由1[2,3]...
分类:
其他好文 时间:
2015-05-10 09:46:21
阅读次数:
125
Next PermutationTotal Accepted: 33595 Total Submissions: 134095Implement next permutation, which rearranges numbers into the lexicographically next gr...
分类:
编程语言 时间:
2015-05-07 18:39:34
阅读次数:
149
【题目】Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not pos...
分类:
其他好文 时间:
2015-05-06 21:04:30
阅读次数:
130
这题,找了好久,都没有找到是什么规律,百度了之后,才知道是第几个最小的排列是枚举排列的第几个。
真是长知识了!!~
知道了这样的规律之后,就可以很快的写出了,algorithm这个头文件中又一个枚举排列的函数next_permutation,第i个最小的排列,就是调用next_permutation i - 1次之后的那个排列。next_permutation同样的适用与可重集,也就...
分类:
其他好文 时间:
2015-05-04 15:37:23
阅读次数:
162
题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列用next_permutation就可以了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #inc...
分类:
其他好文 时间:
2015-05-03 17:11:11
阅读次数:
109
A. Prime Permutation题意:给出一个长为n的字符串,问能否通过重新排列整个字符串,使整个字符串中第i个字符$S_I$与i的素数倍数处的字符$S_prime*i$都相同关键字:[贪心]解法:打出1000内的素数表,统计i位和所有prime*i位相同需要的字符数
分类:
其他好文 时间:
2015-05-03 15:57:11
阅读次数:
107
给一个字符串包含大小写字符,规定'A'
用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂。。
最开始用的set+next_permutation,太年轻,也超时了。。。
运用一个next_permutation()函数即可,头文件
注意要先将字符串sort一遍,然后next_permutation()也要把比较函数cmp传进去,原来都不知道可以三个参数的。。
...
分类:
其他好文 时间:
2015-04-29 21:42:06
阅读次数:
123
求两个字符串的最长公共串。
即统计各字符出现的次数,按字母序枚举,输出该字符两个字符串中出现的较少次数。
#include
#include
int cnt1[50],cnt2[50];
char s1[1010],s2[1010];
int main()
{
int i,j,len1,len2;
while(gets(s1)!=NULL)
{
...
分类:
其他好文 时间:
2015-04-29 21:41:59
阅读次数:
181
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 ...
分类:
其他好文 时间:
2015-04-28 22:50:49
阅读次数:
222
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl...
分类:
其他好文 时间:
2015-04-27 15:07:57
阅读次数:
114