HDU 1027:Ignatius and the Princess II ← next_permutation()

【题目来源】
http://acm.hdu.edu.cn/showproblem.php?pid=1027

【题目描述】
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."

"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?

【题目大意】

给定 N 和 M。
若第一个序列为 1,2,3...N-1,N,第二个序列为 1,2,3...N,N-1,……。求第 M 个排列。


【输入格式】
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.

【输出格式】
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.

【输入样例】
6 4
11 8

【输出格式】
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10

【算法分析】
在使用 next_permutation() 的时候,初始序列一般是一个字典序最小的序列。
如果不是,可以用 sort() 排序,得到最小序列,然后再使用 next_permutation()。

#include <bits/stdc++.h>
using namespace std;const int maxn=100;
int a[maxn];
int n;int main() {cin>>n;for(int i=0; i<n; i++) cin>>a[i];sort(a,a+n);do {for(int i=0; i<n; i++)cout<<a[i]<<" ";cout<<endl;} while(next_permutation(a,a+n));return 0;
}/*
in:
3
1 3 2
out:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/

next_permutation() 用法详见官网:
https://cplusplus.com/reference/algorithm/next_permutation/


【算法代码】

#include <bits/stdc++.h>
using namespace std;const int maxn=1005;
int a[maxn];int main() {int n,m;while(cin>>n>>m) {for(int i=1; i<=n; i++) a[i]=i;int t=1;do {if(t==m) break;t++;} while(next_permutation(a+1,a+1+n));for(int i=1; i<n; i++) cout<<a[i]<<" ";cout<<a[n]<<endl;}return 0;
}/*
in:
6 4
11 8out:
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
*/




【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/118616680
https://blog.csdn.net/hnjzsyjyj/article/details/125808608
https://cplusplus.com/reference/algorithm/next_permutation/



 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/140634.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UITableView的style是UITableViewStyleGrouped

一般情况下&#xff0c;UITableViewStylePlain和UITableViewStyleGrouped是UITableView常用到的style&#xff0c; 之前都是用到的时候&#xff0c;遇到问题直接用度娘&#xff0c;差不多就够用了&#xff0c;今天在修复UI提出的间隙问题&#xff0c;来回改&#xff0c;总觉得…

k8s关于metadata、spec.containers、spec.volumes的属性介绍(yaml格式)

目录 一.metadata常用属性 二.spec.containers子属性介绍 explain pod.spec.containers给出的参考 1.command示例演示 2.env和envFrom示例演示 3.ports部分详解 4.resources部分详解 5.startupProbe格式演示 6.terminationMessagePath和terminationMessagePolicy格式演…

晶圆代工产能利用率下降,降价大战一触即发 | 百能云芯

晶圆代工行业正面临产能利用率的重大挑战&#xff0c;据悉&#xff0c;联电、世界先进和力积电等主要代工厂纷纷降低明年首季的报价&#xff0c;幅度高达两位数百分比&#xff0c;项目客户降幅更高达15%至20%&#xff0c;各大晶圆代工厂深陷产能利用率六成保卫战。 晶圆代工降价…

EDA实验----四选一多路选择器设计(QuartusII)

目录 一&#xff0e;实验目的 二&#xff0e;实验仪器设备 三&#xff0e;实验原理&#xff1a; 四&#xff0e;实验要求 五&#xff0e;实验内容及步骤 1.实验内容 2.实验步骤 六&#xff0e;实验报告 七.实验过程 1.创建Verilog文件&#xff0c;写代码 2.波形仿真 …

获取windows 操作系统 是 win7 还是 win10 32位 或者是 64 位

import platform# 获取操作系统信息 system_info platform.system()# 检测操作系统版本 if "Windows" in system_info:version_info platform.version()if "10" in version_info:print("Windows 10")elif "7" in version_info:print…

如何在苹果iOS系统ipa应用中获取当前版本号和Bundle ID

在iOS应用开发过程中&#xff0c;了解如何获取和使用应用的当前版本号、Bundle ID和其他相关信息是至关重要的。无论是在应用内显示这些信息&#xff0c;还是在编写一些版本依赖的逻辑时&#xff0c;掌握这些知识点都将帮助开发者进行更有效的管理和维护。本文将详细介绍如何在…

解读Sketching the Future (STF):零样本条件视频生成

Diffusion Models视频生成-博客汇总 前言:基于草图的视频生成目前是一个基本无人探索过的领域,videocomposer做过一些简单的探索。Sketching the Future从零样本条件视频生成出发,出色的完成了这一任务。这篇博客就解读一下《Sketching the Future (STF): Applying Conditio…

HarmonyOS应用开发-视频播放器与弹窗

Viedo组件 在手机、平板或是智慧屏这些终端设备上&#xff0c;媒体功能可以算作是我们最常用的场景之一。无论是实现音频的播放、录制、采集&#xff0c;还是视频的播放、切换、循环&#xff0c;亦或是相机的预览、拍照等功能&#xff0c;媒体组件都是必不可少的。以视频功能为…

Python中连接池的分析和应用

连接池&#xff08;Connection Pool&#xff09;是在系统初始化的时候&#xff0c;将网络连接作为对象存储在内存中&#xff0c;当用户需要进行连接时&#xff0c;从连接池中取一个已经建立好的空连接对象&#xff0c;使用完后不关闭连接&#xff0c;将连接放回连接池&#xff…

【Vue原理解析】之虚拟DOM

Vue.js是一款流行的JavaScript框架&#xff0c;它采用了虚拟DOM&#xff08;Virtual DOM&#xff09;的概念来提高性能和开发效率。虚拟DOM是Vue.js的核心之一&#xff0c;它通过在内存中构建一个轻量级的DOM树来代替直接操作真实的DOM&#xff0c;从而减少了对真实DOM的操作次…

暖手宝上架亚马逊美国站UL499报告测试标准要求

暖手宝是运用物理及化学原理研制的自动取暖保健用品。该产品以其自动生热&#xff0c;有趣&#xff0c;实用等新颖独特的优势&#xff0c;深受欢迎——暖手宝具有自动取暖&#xff0c;理疗保健等多种功能。只要插上电源等上10分钟左右就能发热&#xff0c;最后一种是通过锂电池…

【华为OD题库-015】报文重排序-Java

题目 对报文进行重传和重排序是常用的可靠性机制&#xff0c;重传缓冲区内有一定数量的子报文&#xff0c;每个子报文在原始报文中的顺序已知&#xff0c;现在需要恢复出原始报文。 输入描述 输入第一行为N,表示子报文的个数&#xff0c;0<N < 1000。 输入第二行为N个子报…

多线程 浏览器渲染引擎 图形用户界面(GUI,Graphical User Interface)应用程序

目录 多线程浏览器渲染引擎图形用户界面&#xff08;GUI&#xff0c;Graphical User Interface&#xff09;应用程序 &#x1f44d; 点赞&#xff0c;你的认可是我创作的动力&#xff01; ⭐️ 收藏&#xff0c;你的青睐是我努力的方向&#xff01; ✏️ 评论&#xff0c;你的…

【FISCO BCOS】十九、区块链浏览器部署

目录 一、环境依赖 检查环境 1.检查java 二、拉取安装脚本 获取部署安装包 ​编辑 解压安装包 进入目录 三、修改配置 四、部署服务 五、状态检查 检查前后端进程 1.检查后端server进程 2.检查前端的nginx进程 检查进程端口 六、使用区块链浏览器 1.配置群组…

EDA实验-----3-8译码器设计(QuartusII)

目录 一. 实验目的 二. 实验仪器 三. 实验原理及内容 1.实验原理 2.实验内容 四&#xff0e;实验步骤 五. 实验报告 六. 注意事项 七. 实验过程 1.创建Verilog文件&#xff0c;写代码 ​编辑 2.波形仿真 3.连接电路图 4.烧录操作 一. 实验目的 学会Verilog HDL的…

【Java 进阶篇】Java与JQuery:探秘事件绑定、入口函数与样式控制

在现代的Web开发中&#xff0c;Java和JQuery是两个不可或缺的角色。Java为我们提供了强大的后端支持&#xff0c;而JQuery则是前端开发的得力助手。本篇博客将围绕Java和JQuery&#xff0c;深入探讨事件绑定、入口函数和样式控制&#xff0c;带你进入前端开发的奇妙世界。 Jav…

ROS基础—vscode创建工作空间

1、创建ROS工作空间 首先打开ubuntu的终端&#xff0c;接着依次输入如下的命令行&#xff1b; mkdir -p xxx_ws/src(必须得有 src) cd xxx_ws catkin_make当然我一般是新建一个叫做demo的工作空间&#xff0c;如 mkdir -p demo04_ws/src 2、启动vscode cd xxx_ws code . …

粉够荣获淘宝联盟区域理事会常务理事,携手共铸淘客新生态

淘宝联盟区域理事会于2021年成立&#xff0c;首届成立成都、广州、武汉&#xff0c;服务近2000个领军淘宝客企业&#xff0c;作为区域生态与官方交流重要枢纽&#xff0c;理事会举办近百场交流分享会&#xff0c;带动淘客跨域跨业态交流成长。 2023年9月7日第二届淘宝联盟理事…

Unity中关于Lerp()方法的使用

在Unity中&#xff0c;Lerp()方法用于在两个值之间进行线性插值。 它的语法有&#xff1a; public static float Lerp(float a, float b, float t);//在两个float类型的值a和b之间进行线性插值 public static Vector2 Lerp(Vector2 a, Vector2 b, float t);//在两个Vector2类…