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/
相关文章:
HDU 1027:Ignatius and the Princess II ← next_permutation()
【题目来源】http://acm.hdu.edu.cn/showproblem.php?pid1027【题目描述】 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. feng5…...

主题讲座:全球增材制造现状与未来(暨香港科技大学广州|智能制造学域2024博士学位全额奖学金项目)
时间:2023 年11月16日(星期四)14:30 地点:合肥工业大学 学术会议中心三楼报告厅 主讲嘉宾:陈模军 助理教授 https://facultyprofiles.hkust-gz.edu.cn/faculty-personal-page/CHEN-Mojun/mjchen 报名表直达࿱…...
hugeGraph修改PropertyKey属性类型
修改PropertyKey字段属性的类型,发现没办法保留数据的前提下修改,智能是先删除数据,然后再修改,或者备份后修改再恢复。 方法一、 修改groovy脚本中的Text为Int后重新建元数据 schema.propertyKey(“youkey”).asText().valueSing…...

vscode 访问本地或者远程docker环境
1、vscode 访问docker本地环境 直接点击左下角连接图标,弹出选项可以选择容器,只要容器在本地运行者,选择attach可以看到运行中的容器可以选择,选择其中需要选择的就行 ## 运行容器,可以-d后台运行都可以 docker run…...

人工智能与充电技术:携手共创智能充电新时代
人工智能与充电技术:携手共创智能充电新时代 摘要:本文探讨了人工智能与充电技术的结合及其在未来充电设施领域的应用。通过分析智能充电系统的技术原理、优势以及挑战,本文展望了由人工智能驱动的充电技术为未来电动交通带来的巨大变革与机…...
『自定义B站视频播放速度』
哔哩哔哩 的最高播放速度是 2.0, 但对于我们这种程序员来说,2.0 速度观看学习视频还是稍微慢了点, 🫵🏻3.0 以上才是王道🫵🏻, 下面就是具体的操作方法: ① 在浏览器…...

Java入门篇 之 继承
本篇碎碎念:最近的课程遇到瓶颈了,看的时候感觉自己会了,但是结束仔细一回顾还是一知半解,一点一点来吧,基础必须要打好(自己给自己好的心里暗示,结局一定是好的) 今日份励志文案:慢慢改变,慢慢…...

如何计算掩膜图中多个封闭图形的面积
import cv2def calMaskArea(image,idx):mask cv2.inRange(image, idx, idx)contours, hierarchy cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)for contour in contours:area cv2.contourArea(contour)print("图形的面积为", area) image是…...

【Nginx】CentOS 安装Nignx
CentOS上安装Nginx: 1. 打开终端:使用SSH或者直接在服务器上打开终端。 2. 更新系统:运行以下命令以确保您的系统软件包列表是最新的: sudo yum update3. 安装Nginx:运行以下命令以安装Nginx: sudo yum…...
idea 代码快捷键Mac版
1、查询任何东西 双击 Shift2、文件内查找 Command F 3、文件内替换 Command R4、全局查找(根据路径) Command Shift F5、在当前文件跳转到某一行的指定处 Command L6、退回 / 前进到上一个操作的地方 Command Option 方向键左Command Opt…...

【NI-DAQmx入门】多通道数据采集
1.通道扩展解释 通道扩展是扩展数据采集设备的通道以包含另一个设备的通道的过程,从而有效地创建具有更多通道的任务。当使用通道扩展时,DAQmx 自动在 DAQmx 驱动程序级别路由触发器和时钟,以便多个设备同步。为了使设备作为一个整体运行&…...

回顾 — SFA:简化快速 AlexNet(模糊分类)
模糊图像的样本 一、说明 在本文回顾了基于深度学习的模糊图像分类(SFA)。在本文中:Simplified-Fast-AlexNet (SFA)旨在对图像是否因散焦模糊、高斯模糊、雾霾模糊或运动模糊而模糊进行分类。 二、大纲 图像模糊建模简要概述简化快速 AlexNet…...

基于51单片机PCF8591数字电压表数码管显示设计( proteus仿真+程序+设计报告+讲解视频)
PCF8591数字电压表数码管显示 1.主要功能:讲解视频:2.仿真3. 程序代码4. 设计报告5. 设计资料内容清单&&下载链接资料下载链接(可点击): 基于51单片机PCF8591数字电压表数码管设计( proteus仿真程序设计报告讲…...

分发饼干(贪心算法+图解)
455. 分发饼干 - 力扣(LeetCode) 题目描述 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最…...

vue项目路由使用history模式,nginx配置,刷新页面显示404
需要在配置项中添加 try_files $uri $uri/ /index.html;...
redis的redis.service配置
在CentOS中,可以使用以下步骤配置redis.service: 创建redis用户和组 在终端中执行以下命令: 复制插入 sudo useradd -r -s /bin/false redis复制插入 这将创建一个名为redis的系统用户,并禁止该用户登录系统。 安装Redis 在…...

高频SQL50题(基础版)-3
文章目录 主要内容一.SQL练习题1.1174-即时食物配送代码如下(示例): 2.550-游戏玩法分析代码如下(示例): 3.2356-每位教师所教授的科目种类的数量代码如下(示例): 4.1141-查询近30天活跃用户数代码如下&…...

OpenMMlab导出yolov3模型并用onnxruntime和tensorrt推理
导出onnx文件 直接使用脚本 import torch from mmdet.apis import init_detector, inference_detectorconfig_file ./configs/yolo/yolov3_mobilenetv2_8xb24-ms-416-300e_coco.py checkpoint_file yolov3_mobilenetv2_mstrain-416_300e_coco_20210718_010823-f68a07b3.pth…...
单链表的插入删除
#include <iostream>#include <stdio.h> #include <stdlib.h>using namespace std;//带头指针的单链表typedef struct LNode{int data;struct LNode *next;}LNode, *LinkList;bool InitList(LinkList &L){L (LNode *) malloc(sizeof(LNode));if(L NUL…...

github使用手册
核心代码 配置用户名/邮箱 best practice git init #在本地初始化一个仓库 git add . #将当前目录所有的文件加入(注意这里是加入)到缓存区 git commit -m "xxx" #将当前缓存区里的内容提交到本地仓库 git remote add <remote_rep_name&g…...

CTF show Web 红包题第六弹
提示 1.不是SQL注入 2.需要找关键源码 思路 进入页面发现是一个登录框,很难让人不联想到SQL注入,但提示都说了不是SQL注入,所以就不往这方面想了 先查看一下网页源码,发现一段JavaScript代码,有一个关键类ctfs…...

阿里云ACP云计算备考笔记 (5)——弹性伸缩
目录 第一章 概述 第二章 弹性伸缩简介 1、弹性伸缩 2、垂直伸缩 3、优势 4、应用场景 ① 无规律的业务量波动 ② 有规律的业务量波动 ③ 无明显业务量波动 ④ 混合型业务 ⑤ 消息通知 ⑥ 生命周期挂钩 ⑦ 自定义方式 ⑧ 滚的升级 5、使用限制 第三章 主要定义 …...

华为OD机试-食堂供餐-二分法
import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南 在数字化营销时代,邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天,我们将深入解析邮件打开率、网站可用性、页面参与时…...
A2A JS SDK 完整教程:快速入门指南
目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库ÿ…...
【JavaSE】多线程基础学习笔记
多线程基础 -线程相关概念 程序(Program) 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序,比如我们使用QQ,就启动了一个进程,操作系统就会为该进程分配内存…...

[论文阅读]TrustRAG: Enhancing Robustness and Trustworthiness in RAG
TrustRAG: Enhancing Robustness and Trustworthiness in RAG [2501.00879] TrustRAG: Enhancing Robustness and Trustworthiness in Retrieval-Augmented Generation 代码:HuichiZhou/TrustRAG: Code for "TrustRAG: Enhancing Robustness and Trustworthin…...
上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
简介 在我的 QT/C 开发工作中,合理运用设计模式极大地提高了代码的可维护性和可扩展性。本文将分享我在实际项目中应用的三种创造型模式:工厂方法模式、单例模式和生成器模式。 1. 工厂模式 (Factory Pattern) 应用场景 在我的 QT 项目中曾经有一个需…...
在RK3588上搭建ROS1环境:创建节点与数据可视化实战指南
在RK3588上搭建ROS1环境:创建节点与数据可视化实战指南 背景介绍完整操作步骤1. 创建Docker容器环境2. 验证GUI显示功能3. 安装ROS Noetic4. 配置环境变量5. 创建ROS节点(小球运动模拟)6. 配置RVIZ默认视图7. 创建启动脚本8. 运行可视化系统效果展示与交互技术解析ROS节点通…...
Windows 下端口占用排查与释放全攻略
Windows 下端口占用排查与释放全攻略 在开发和运维过程中,经常会遇到端口被占用的问题(如 8080、3306 等常用端口)。本文将详细介绍如何通过命令行和图形化界面快速定位并释放被占用的端口,帮助你高效解决此类问题。 一、准…...