CF513B1 Permutations 题解
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 题解的更多相关文章
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- 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从 ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- CodeForces 340E Iahub and Permutations 错排dp
Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...
- LeetCode编程训练 - 回溯(Backtracking)
回溯基础 先看一个使用回溯方法求集合子集的例子(78. Subsets),以下代码基本说明了回溯使用的基本框架: //78. Subsets class Solution { private: voi ...
- 算法与数据结构基础 - 回溯(Backtracking)
回溯基础 先看一个使用回溯方法求集合子集的例子(78. Subsets),以下代码基本说明了回溯使用的基本框架: //78. Subsets class Solution { private: voi ...
- [LeetCode 题解]: Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- [LeetCode 题解]: Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
随机推荐
- 5、使用ZSetOperations(有序)操作redis(Zset有序集合)
文章来源:https://www.cnblogs.com/shiguotao-com/p/10564454.html 方法 c参数 s说明 void set(K key, V value); ke ...
- dotnet 将自动代码格式化机器人带入团队 GitLab 平台
给团队带入一个 代码格式化机器人 能提升团队的幸福度,让团队的成员安心写代码,不用关注代码格式化问题,将格式代码这个粗活交给机器人去做.同时也能减少在代码审查里撕格式化问题的时间,让更多的时间投入到更 ...
- SNPEFF snp注释 (添加自己基因组)
之间介绍过annovar进行对snp注释,今天介绍snpEFF SnpEff is a variant annotation and effect prediction tool. It annota ...
- 【4】蛋白质组学鉴定软件之MSGFPlus
目录 1.简介 2.安装运行 3.结果 1.简介 MSGF+也是近年来应用得比较多的蛋白鉴定软件.java写的,2008年初次发表JPR,2014年升级发表NC,免费开源,持续更新维护,良心软件.而且 ...
- BeautifulSoup解析库的介绍和使用
### BeautifulSoup解析库的介绍和使用 ### 三大选择器:节点选择器,方法选择器,CSS选择器 ### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文 ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Spring DAO
Spring DAO 连接池 使用JDBC访问数据库是,频繁的打开连接和关闭连接,造成性能影响,所以有了连接池.数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接, ...
- 数据集成工具—Sqoop
数据集成/采集/同步工具 @ 目录 数据集成/采集/同步工具 Sqoop简介 Sqoop安装 1.上传并解压 2.修改文件夹名字 3.修改配置文件 4.修改环境变量 5.添加MySQL连接驱动 6.测 ...
- 备忘录:关于.net程序连接Oracle数据库
目录 关于使用MSSM访问Oracle数据库 关于. net 程序中连接Oracle数据库 志铭-2021年12月7日 21:22:15 关于使用MSSM访问Oracle数据库 安装访问接口组件:Or ...
- HashMap有几种遍历方法?推荐使用哪种?
本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...