AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

HDU 1027. Ignatius and the Princess II(next_permutation应用)    原题链接    简单

作者: 作者的头像   ZhinKuojia ,  2024-09-05 19:03:47 ,  所有人可见 ,  阅读 9


0


next_permutation应用

STL 提供求下一个排列组合的函数 next_permutation()。例如 3 个字符 a, b, c 组成的序列,next_permutation() 能按字典返回 6 个组合,即 abc, acb, bac, bca, cab, cba

next_permutation() 的定义有下面两种形式:

  • bool next_permutation(BidirectionalIterator first, BidirectionalIterator last);

  • bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Comparecomp);

返回值:如果没有下一个排列组合,返回false,否则返回true。每执行一次,就会把新的排列放到原来的空间里

样例:Ignatius and the Princess II (HDU 1207)

给定 n 个数字,从 1 到 n ,要求输出第 m 小的序列。

输入:数字 n 和 m ,1 <= n <= 1000, 1 <= m <= 10000;

输出:输出第 m 小的序列

Sample Input

6 4
11 8

Sample Output

1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10

思路: 首先生成一个 1 2 … n 的最小字典序列,然后用 next_permutation() 一个一个生成字典序更大的序列

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;

int a[N];
int n, m;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while(cin >> n >> m){
        for(int i = 1; i <= n; i ++) a[i] = i; // 生成一个字典序最小的序列
        int k = 0;
        do{
            if(k == m) break;
            k ++;
        }
        while(next_permutation(a + 1, a + 1 + n));
        for(int i = 1; i <= n; i ++){
            cout << a[i] << ' ';
        }
        cout << '\n';
    }


    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息