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)\)代表每一行多少人,判断边界就能 ...
随机推荐
- k8s-控制器deployment弹性扩容,更新镜像,回滚,DaemonSet,StatufluSet
目录 1.控制器deployment,DaemonSet,StatufluSet 2.控制器-deployment 弹性扩容 方式1-修改配置清单 方式2-打标签 方式3-scale 更新镜像 方式1 ...
- vue项目中使用canvas
canvas API 文档:https://www.canvasapi.cn/ 一.在html中使用canvas canvas 元素用于在网页上绘制图形. 在html中,使用 document.ge ...
- 【BZOJ 4668 冷战】
题目: [BZOJ 4668 冷战] 思路: 因为考虑强制在线,我们是肯定要维护形状的 我们发现如果\((u,v)\)这条边如果\(u,v\)已经连上,那么对于最终答案这条边是没有贡献的 所以我们发现 ...
- Codeforces Gym 101480C - Cow Confinement(扫描线+线段树)
题面传送门 题意: 有一个 \(10^6\times 10^6\) 的地图.其中 \(m\) 个位置上有花,\(f\) 个矩形外围用栅栏围了起来.保证 \(f\) 个矩形两两之间没有公共点. \(q\ ...
- R语言与医学统计图形-【23】ggplot2坐标系转换函数
ggplot2绘图系统--坐标系转换函数 包括饼图.环状条图.玫瑰图.戒指图.坐标翻转. 笛卡尔坐标系(最常见). ArcGIS地理坐标系(地图). Cartesian坐标系. polar极坐标系. ...
- ansible-playbook 安装redis 主从
ansible-playbook 安装redis 主从 手动在测试机上安装一遍redis,最好使用utils下面的install_server.sh安装服务,然后将redis的配置文件和init需要的 ...
- LeetCode二维数组中的查找
LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...
- python下载openpyxl
直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...
- Linux基础命令---mysqlimport导入数据库
mysqlimport mysqlimport指令可以用来将文本文件中的数据导入到数据库.在导入文本文件的时候,必须确保数据库中有一张表,而且他的名字和文本文件的名字是一样的. 此命令的适用范围:Re ...
- MySQL(3):SELECT语法
一,数据库语句 数据库数据是通过DML语句管理数据库数据,包括: INSERT (添加数据语句) UPDATE (更新数据语句) DELETE (删除数据语句) 1:INSERT (添加数据语句) I ...