直接选择序列的方法解本题,可是最坏时间效率是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. somethings about QSplitter

    m_splitter = new QSplitter(Qt::Horizontal);    m_splitter->addWidget(this->m_leftWidget);    m ...

  2. Java基础知识强化68:基本类型包装类之Character概述和Character常见方法

    1. Character概述: public final class Character extends Object implements Serializable,Comparable<Ch ...

  3. C#高级知识点概要(2) - 线程并发锁

    本文目录: 线程的简单使用 并发和异步的区别 并发控制 - 锁 线程的信号机制 线程池中的线程 案例:支持并发的异步日志组件 线程的简单使用 常见的并发和异步大多是基于线程来实现的,所以本文先讲线程的 ...

  4. 武汉科技大学ACM :1005: 零起点学算法101——手机短号

    Problem Description 大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号.假设所有的短号都是是 6+手机号的后5位,比 ...

  5. C++ Primer 5th 第2章 变量和基本类型

    *****代码在Debian g++ 5.3.1 / clang++ 3.8(C++11)下编写调试***** 由于部分编译器对标准遵循的不同以及自身额外的扩展,本章书中的少数知识点与实际实现存在偏差 ...

  6. 腾讯的一道js面试题(原型)

    有一只小狗叫花花,它会“汪汪”叫,他的同伴也会汪汪叫,后来环境发生了变化,新出生的狗不会再“汪汪”叫,而变成“呜呜”叫. 试通过继承来达到目的 function Dog(){ 2 this.bark ...

  7. 工作中遇到的浏览器差别(就不叫IE6bug了)

    1.根据ie版本写css <!--[if lt IE 8]> <style> .cntContainer{margin-top: -1px;} </style> & ...

  8. jQuery Lazy Load 图片延迟加载

    基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...

  9. 使用APMServ本地搭建多个网站

    October 27, 2014 使用APMServ本地搭建多个网站教程 把我写好的代码直接粘贴到 httpd.conf 文件的末尾.然后保存就可以了.代码如下: <VirtualHost *: ...

  10. HTML5 中 div section article 的区别

    刚刚开始接触 HTML5 时,对它的标签很不适应,甚至一度有点反感.尤其是对 div.section.article 这几个标签,实在弄不清楚应该使用在什么场合下. div HTML Spec: Th ...