直接选择序列的方法解本题,可是最坏时间效率是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. 基本SQL语句练习之SELECT

    一.SQL Plus连接sqlplus:以命令行方式连接数据库sqlplusw:以窗口登录方式连接数据库conn sys/password as sysdba;show userselect * fr ...

  2. jquery之onchange事件2

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 2、第2节课html教程客户端控件/css第一课/20150917

    1.<form> 标签 提交 <form action="http://www.baidu.com" method="post'> </fo ...

  4. jsp DAO设计模式

    DAO(Data Access Objects)设计模式是属于J2EE体系架构中的数据层的操作. 一.为什么要用DAO? 比较在JSP页面中使用JDBC来连接数据库,这样导致了JSP页面中包含了大量的 ...

  5. (转)ip地址,手机ip查询

    页面地址:http://www.ip138.com/ 外链地址(实际主页面里面有)http://www.ip138.com/iplink.htm 外链地址里面的内容: <FORM METHOD= ...

  6. div+css不间断滚动字幕

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. sublime3配置及插件安装

    1.下载https://github.com/wbond/sublime_package_control中的zip文件,解压后将文件夹名更改为Package Control. 2.将1中的文件夹放入s ...

  8. 17--Box2D使用(三、触摸交互)

    Box2D引擎与触摸的交互通过创建鼠标关节以及碰撞检测来得到触摸点下面的刚体,在根据触摸操作完成相应的功能.首先添加触摸响应函数声明 virtual void ccTouchesBegan(cocos ...

  9. Jquery简略API使用

    都是个人随手笔记,既然开通了博客园就分享给大家.谨做为参考,具体大家自己测试以及使用 ★ $() ★ JQ的一个万能获取对象的函数(获取跟CSS获取元素是一样的)$(function(){}); 替代 ...

  10. python 中变量的命名规范

    出自:http://www.diybl.com/course/3_program/python/20111130/563643.html 模块名: 小写字母,单词之间用_分割 ad_stats.py ...