Content

给定两个整数 \(n,m\)。定义 \(f(p)=\sum\limits_{l=1}^n\sum\limits_{r=l}^n\min\limits_{i=l}^rp_i\),其中 \(p\) 为一个长度为 \(n\) 的排列。现在,请你求出所有使得 \(f(p)\) 最大的长度为 \(n\) 的排列中,字典序第 \(m\) 小的排列。

数据范围:\(1\leqslant n\leqslant 8\)。

Solution

看到数据范围马上想到一种很 naive 的 \(O(n!\cdot n^3)\) 的做法:先枚举所有的排列求出最大的 \(f(p)\),然后再枚举所有的排列扫到使得 \(f(p)\) 最大的字典序第 \(m\) 小的排列。

next_permutation 可以更方便地枚举全排列,具体看代码。

Code

namespace Solution {
const int N = 17;
int n, m, mx, p[N]; iv Main() {
read(n, m); F(int, i, 1, n) p[i] = i;
do {
int sum = 0;
F(int, l, 1, n) F(int, r, l, n) {
int mn = 10;
F(int, i, l, r) mn = min(mn, p[i]);
sum += mn;
}
mx = max(mx, sum);
}while(next_permutation(p + 1, p + n + 1));
int cnt = 0;
F(int, i, 1, n) p[i] = i;
do {
int sum = 0;
F(int, l, 1, n) F(int, r, l, n) {
int mn = 10;
F(int, i, l, r) mn = min(mn, p[i]);
sum += mn;
}
if(sum == mx) {
cnt++;
if(cnt == m) {
F(int, i, 1, n) printf("%d%c", p[i], " \n"[i == n]);
break;
}
}
}while(next_permutation(p + 1, p + n + 1));
return;
}
}

CF513B1 Permutations 题解的更多相关文章

  1. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  2. POJ P2279 Mr. Young's Picture Permutations 题解

    每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...

  3. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  4. CodeForces 340E Iahub and Permutations 错排dp

    Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...

  5. LeetCode编程训练 - 回溯(Backtracking)

    回溯基础 先看一个使用回溯方法求集合子集的例子(78. Subsets),以下代码基本说明了回溯使用的基本框架: //78. Subsets class Solution { private: voi ...

  6. 算法与数据结构基础 - 回溯(Backtracking)

    回溯基础 先看一个使用回溯方法求集合子集的例子(78. Subsets),以下代码基本说明了回溯使用的基本框架: //78. Subsets class Solution { private: voi ...

  7. [LeetCode 题解]: Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. [LeetCode 题解]: Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. 【题解】POJ2279 Mr.Young′s Picture Permutations dp

    [题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...

随机推荐

  1. SpringCloud微服务实战——搭建企业级开发框架(二十一):基于RBAC模型的系统权限设计

    RBAC(基于角色的权限控制)模型的核心是在用户和权限之间引入了角色的概念.取消了用户和权限的直接关联,改为通过用户关联角色.角色关联权限的方法来间接地赋予用户权限,从而达到用户和权限解耦的目的. R ...

  2. Error occurred during initialization of VM Could not reserve enough space fo

    通过es的elasticsearch.bat 启动.发现错误:Error occurred during initialization of VM Could not reserve enough s ...

  3. 9.1 k8s pod版本更新流程及命令行实现升级与回滚

    1.创建 Deployment root@k8-deploy:~/k8s-yaml/controllers/deployments# vim nginx-deployment.yaml apiVers ...

  4. [APIO2020]有趣的旅途

    注意到第一个点是可以钦定的. 那么我们考虑在重心的子树里反复横跳. 每次选择不同子树里的深度最大的点. 在同一颗子树里可能会在lca处出现问题. 那么我们选择重心,要考虑到会不会出现一颗子树不够选的操 ...

  5. 【2020五校联考NOIP #7】道路扩建

    题面传送门 题意: 给出一张 \(n\) 个点 \(m\) 条边的无向图 \(G\),第 \(i\) 条边连接 \(u_i,v_i\) 两个点,权值为 \(w_i\). 你可以进行以下操作一次: 选择 ...

  6. Run For Beer CF575G

    Run for beer CF 575G 如果直接bfs分层贪心可以做,但是很毒瘤,具体可以参考Gavinzheng的提交 考虑魔改dijkstra 首先,每次拿权值最小的来松弛肯定没有问题,只是怎么 ...

  7. 关闭 IDEA 自动更新

    关闭 IDEA 的自动检查更新(截图idea 2020 2.x) idea 右下角会有这样的更新提示 2. 关闭 idea 自动检查更新 取消勾选 Automatically check update ...

  8. Glib 对 C 函数进行单元测试

    1. Glib 单元测试框架 Glib 为单元测试提供了一套完整的测试框架,每个测试运行包括以下几个部分 测试数据结构 测试 setup 与 teardown 函数 测试函数 2. 单元测试数据结构 ...

  9. Hermite WENO 重构格式

    Hermite WENO 单元重构 本文主要介绍采用 Hermite WENO 重构方法作为斜率限制器应用于二维或高维单元中. 1.简介[1] ENO格式最早由 Harten 等[2]提出,ENO格式 ...

  10. 汽车C2M模式综述