HDU 1027 Ignatius and the Princess II 选择序列题解
直接选择序列的方法解本题,可是最坏时间效率是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 选择序列题解的更多相关文章
- 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 ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU - 1027 Ignatius and the Princess II 全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 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 ( ...
- HDU 1027 - Ignatius and the Princess II
第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...
- HDU 1027 Ignatius and the Princess II 排列生成
解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- 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 ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
随机推荐
- Eclipse 添加快捷方式
1.在/usr/share/applications创建一个desktop文件,命名为eclipse.desktop 文件内容如下 [Desktop Entry]Name=EclipseType=Ap ...
- sql server里一些常用的查询
查看表的创建和更改时间: select * from sys.tables 查询数据库的创建时间: select * from sys.databases where name in ('数据 ...
- (转)HiddenField控件的使用
ASP.NET2.0 HiddenField控件(1)2007-05-12 23:18HiddenField控件顾名思义就是隐藏输入框的服务器控件,它能让你保存那些不需要显示在页面上的且对安全性要求不 ...
- git 笔记记录
分布式版本控制系统Git 是一套内容寻址文件系统,从核心上来看不过是简单地存储键值对.一: git 本地clone 一个仓库 1. 直接clone一个仓库: $: git clon ...
- OpenGL ES 2.0 顶点着色器的妙用
1.飘扬的旗帜(水面起伏) 基本原理 绘制一帧画面时由顶点着色器根据一定的规则变换各个顶点的位置,即可得到旗帜迎风飘扬的效果. 为了使旗帜的飘动过程比较平滑,采用基于正弦曲线的顶点位置变换规则.
- zeromq源码分析笔记之架构(1)
1.zmq概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接字.引用云风的话来说:ZeroMQ 并不是一个对 so ...
- Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1
linux空设备文件和重定向 输出/输入重导向 > >> < << :> &> 2&> 2< ...
- Python 多线程 队列 示例
Python3,开一个线程,间隔1秒把一个递增的数字写入队列,再开一个线程,从队列中取出数字并打印到终端 #! /usr/bin/env python3 import time import thre ...
- /dev/socket/vold exploit 本地提权漏洞
EXPLOIT "0 asec create ../../../../../../../../xxxxx/xx/xx/xx 1 ext4 98235792350852308254872354 ...
- Spring MVC使用@ResponseBody返回JSON数据406以及乱码问题解决方案
刚到一家公司,做原始的工作---接口,程序就我一人,没环境,没架构,更没旧项目可以利用,一切从0开始,经理以为我是老鸟,来就布置任务,要一周拿出结果.其实有现成架构的话写个接口还不是分分钟钟的事,关键 ...