直接选择序列的方法解本题,可是最坏时间效率是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. 将EXCEL中的列拼接成SQL insert插入语句

    工作中经常需要将EXCEL文件中的数据导入到各种数据库,但是对于不熟悉数据库的人来说,如果直接使用命令执行导入,这无疑是一个难题,也是一个风险.这里我们直接在EXCEL文件中拼接成标准的SQL ins ...

  2. 使用vs中的工具进行架构比较

    使用vs自带的架构比较工具可以对不同库中的结构进行比较,也可以将源中的架构更新到目标架构中.当然这种更新只是架构的更新,数据并不会同步.

  3. jQuery选择器 之详述

    jQuery选择器 一. 单词小计 Pervious 上一页sibling  同级first  第一last  最后not  不 Even  偶数    odd  奇数 header  页眉 一.jQ ...

  4. [C#]asp.net开发微信公众平台----目录汇总-持续更新

    1.[c#]asp.net微信公众平台开发(1)数据库设计 2.[c#]asp.net微信公众平台开发(2)多层架构框架搭建和入口实现 3.[c#]asp.net微信公众平台开发(3)微信消息封装及反 ...

  5. lvs+keepalived+nginx+tomcat

    # 拓扑如下所示 # 节点分布情况 LVS-dr-master eth0: 192.168.146.141 LVS-dr-slave eth0: 192.168.146.142 nginx1: eth ...

  6. Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)

    原文:InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序) InstallShield Limited Edi ...

  7. J2EE学习记录,EJB,JNDI,RMI

    Java EE 是java平台企业版(Java Platform Enterprise Edition)缩写,是Sum公司为企业级应用推出的标准平台. 随着Java技术的发展,J2EE平台得到了迅速的 ...

  8. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  9. uva 498 - Polly the Polynomial

    UVa 498: Polly the Polynomial | MathBlog #include <cstdio> #include <cstdlib> using name ...

  10. jQuery源码分析1

    写在开头: 昨天开始,我决定要认真的看看jQuery的源码,选择1.7.2,源于公司用的这个版本.由于源码比较长,这将会是一个比较持久的过程,我将要利用业余时间,和偶尔上班不算忙的时间来进行.其实原本 ...