直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS。

使用删除优化,那么就能够达到0MS了。

删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针就能够了,那么时间效率就是O(1)了,而普通的删除那么时间效率是O(n),故此大大优化了程序。

怎样直接选择第k个序列,能够參考本博客的Leetcode题解。Leetcode题有个一模一样的题目。只是没有使用删除优化。

看见本题的讨论中基本上都是使用STL解,还有沾沾自喜的家伙,只是使用STL解决本题尽管是能够,可是那是由于本题的数据非常弱;

由于使用STL的时间效率是O(n*m),当中n可能是1000, 而m可能是10000,故此会达到1千万的数据处理,随便添加个大数据用例就会超时。

故此使用STL来解决本题事实上是非常次,非常0基础的解法了。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std; const int MAX_N = 1001;
int arr[MAX_N], N, M, tbl[MAX_N]; void genSeqNum()
{
int mth = M-1;
memset(tbl, 0, sizeof(int) * N);
tbl[N-1] = 0;
for (int i = N-2, d = 2; i >= 0 && mth > 0; i--, d++)
{
tbl[i] = mth % d;
mth /= d;
}
} void eraseNth(int A[], int i)
{
--N;
for (; i < N; i++)
{
A[i] = A[i+1];
}
} void printNums()
{
genSeqNum(); int *A = arr;
printf("%d", A[tbl[0]]);
if (!tbl[0]) A++, N--;
else eraseNth(A, tbl[0]); int t = 1;//定位tbl下标
while (N > 0)//优化之后的算法
{
for (; N && !tbl[t]; t++)//主要优化地方
{
printf(" %d", *A);//输出为零的,不用使用删除函数
A++, N--;//又一次定位数列
}
if (!N) break;//已经输出完成 printf(" %d", A[tbl[t]]);//不为零的选择,使用删除函数
eraseNth(A, tbl[t]);
t++;
}
putchar('\n');
} int main()
{
while (scanf("%d %d", &N, &M) != EOF)
{
for (int i = 0; i < N; i++)
{
arr[i] = i+1;
}
printNums();
}
return 0;
}

HDU 1027 Ignatius and the Princess II 选择序列题解的更多相关文章

  1. 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/10 ...

  2. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  3. HDU - 1027 Ignatius and the Princess II 全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  4. 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 ( ...

  5. HDU 1027 - Ignatius and the Princess II

    第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...

  6. HDU 1027 Ignatius and the Princess II 排列生成

    解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...

  7. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  8. hdu 1027 Ignatius and the Princess II(正、逆康托)

    题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...

  9. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

随机推荐

  1. javascript 知识点坑

    1. JavaScript事件属性 event.target 当目标事件发生span里面 当目标事件发生在main里面 e.target; // 目标节点DOM结构   e.target.id; // ...

  2. jquery之营销系统(会员促销)

    var appPath = getAppPath(); var cnt = 0; var loadCnt = 0; $(function() { $("#opreateHtml") ...

  3. js字符串转json

    1,eval方式解析,这是最早的解析方式了.如下: 代码如下: function strToJson(str){ var json = eval('(' + str + ')'); return js ...

  4. MVC 5 下,应用log4net收集异常信息

    1.安装log4net. 首先在VS中通过nuget安装logenet,我用的是VS2013,截屏如下:

  5. memcached原理全面剖析

    memcached会预先分配内存,memcached分配内存方式称之为allocator, 首先,这里有3个概念: 1 slab 2 page 3 chunk 一般来说一个memcahced进程会预先 ...

  6. Swift--集合类型 数组 字典 集合

    数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...

  7. Kendo Web UI Grid添加一个html控件如(checkbox,button)

    在Kendo Web UI Grid增加一个控件如效果图: <div id="grid1"></div><script> $("#gr ...

  8. Python番外之 阻塞非阻塞,同步与异步,i/o模型

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端: 同步:      所谓同步,就 ...

  9. JS 操作一个数据值

    任何语言都有自己的操作数据的方法: Js也不例外,js有3种重要的方式来操作一个数据值. 1>复制它.例如把它赋给一个新的变量. 2>把它作为参数传递给一个函数或方法. 3>可以和其 ...

  10. Unity3d有关图形尺寸大小的注意事项

    主要参考了官方文档,然后根据个人的理解撰写该文.Unity3D支持的图形文件格式有 PSD, TIFF, JPG, TGA, PNG, GIF, BMP, IFF, PICT(但根据本人的亲手测试,U ...