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)\)代表每一行多少人,判断边界就能 ...
随机推荐
- 力扣 - 剑指 Offer 10- I. 斐波那契数列
题目 剑指 Offer 10- I. 斐波那契数列 思路1(递归 / 自顶向下) 这题是很常见的一道入门递归题,可以采用自顶向下的递归方法,比如我们要求第n个位置的值,根据斐波那契数列的定义fib(n ...
- Android连接远程数据库的避坑指南
Android连接远程数据库的避坑指南 今天用Android Studio连接数据库时候,写了个测试连接的按钮,然后连接的时候报错了,报错信息: 2021-09-07 22:45:20.433 705 ...
- 全面了解一致性哈希算法及PHP代码实现
在设计一个分布式系统的架构时,为了提高系统的负载能力,需要把不同的数据分发到不同的服务节点上.因此这里就需要一种分发的机制,其实就是一种算法,来实现这种功能.这里我们就用到了Consistent Ha ...
- 多线程Reactor模式
目录 1.1 主服务器 2.1 IO请求handler+线程池 3.1 客户端 多线程Reactor模式旨在分配多个reactor每一个reactor独立拥有一个selector,在网络通信中大体设计 ...
- 【R】clusterProfiler的GO/KEGG富集分析用法小结
前言 关于clusterProfiler这个R包就不介绍了,网红教授宣传得很成功,功能也比较强大,主要是做GO和KEGG的功能富集及其可视化.简单总结下用法,以后用时可直接找来用. 首先考虑一个问题: ...
- 6. Reverse Linked List 逆转单链表
逆转单链表,比较简单,不细讲,扫描依次改变指针指向. class Solution { public: ListNode* reverseList(ListNode* head) { if(head= ...
- C++类成员初始化列表的构造顺序
看下面代码, 输出结果是多少呢? class A{ public: A(int k) : j(k), i(j) { } void show() { cout << this->i & ...
- 判断是否有重复,判断字符串是否有重复汉字【c#】
string corn = "公司"; int n = 0; if (tbCorporateName.Text.IndexOf(corn) > -1) { string co ...
- vim文本编辑器的基本使用
vim文本编辑器的基本使用 1. vi和vim的区别和联系 可以说vim是vi的增强版,在使用vim编辑文本时,可以根据字体颜色来判断编写程序的正确性. 2. vim文本编辑器的常用命令 1. 编辑指 ...
- Android系统编程入门系列之硬件交互——多媒体麦克风
在多媒体摄像头及相关硬件文章中,对摄像头的使用方式需要区分应用程序的目标版本以使用不同的代码流程,而与之相比,麦克风硬件的使用就简单多了. 麦克风及相关硬件 麦克风硬件在移动设备上作为音频的采集设备, ...