cf886E Maximum Elements 题解
来源:互联网 发布:常州茗顺网络 编辑:程序博客网 时间:2024/06/05 00:11
DP神题。也是本蒟蒻做的第一道组合数学+DP题。
先来看看题意。Petya嫌弃自己求区间最大值的做法太慢了,所以发明了这样一个函数:
int fast_max(int n, int a[]) { int ans = 0; int offset = 0; for (int i = 0; i < n; ++i) if (ans < a[i]) { ans = a[i]; offset = 0; } else { offset = offset + 1; if (offset == k) return ans; } return ans;}
显然这个函数很容易被hack掉,现在简化问题:a数组是1~n的排列,输入n,k,问有多少情况会被hack掉。
比如说,n = 5, k = 3,那么[4, 1, 2, 3, 5], [4, 1, 3, 2, 5], [4, 2, 1, 3, 5], [4, 2, 3, 1, 5], [4, 3, 1, 2, 5], [4, 3, 2, 1, 5]是会被hack掉的。
一上来我看见了dp的标签,就在思考前i个数能够hack掉的情况有多少。但这样转移是很难设计的,于是我就厚颜无耻的看了题解。鉴于我辣鸡的阅读理解水平,还是没有理解。
于是我机智的翻了讨论区。
首先,我们用dp[i]表示1~i的排列能够hack掉的数量是多少。
不得不说,这是一个非常巧妙的状态设计,因为借此问题就从一道玄学题变成了组合数学题。
再思考转移。那么目前横亘在我们面前的就是:i是否被作为最大值扔出去?
分类讨论一下。
我们称一个符合条件的排列
如果
否则,不妨设
综上所述,我们可以列出一个DP方程:
这里可以考虑用前缀和维护一下
接下来转换一下思维。考虑1~i的排列中i的位置,如果i在第k位置这个排列是好的,那么就有
可以将后面那一串用前缀和维护一下,则复杂度
至此,该问题解决。代码之后再填吧..
- cf886E Maximum Elements 题解
- CF886E Maximum Element
- Maximum Difference Between Two Elements
- LeetCode题解:Maximum Subarray
- LeetCode题解:Maximum Subarray
- LeetCode - Maximum Gap 题解
- Maximum Gap|leetcode题解
- LeetCode题解:Maximum Subarray
- LeetCode53. Maximum Subarray题解
- cf870c Maximum splitting 题解
- [LeetCode]Maximum Subarray题解
- LeetCode题解:Remove Linked List Elements
- LeetCode 题解(199) : Remove Linked List Elements
- LeetCode题解:Remove Linked List Elements
- LeetCode题解-203-Remove Linked List Elements
- Leetcode题解 347. Top K Frequent Elements
- leetcode题解-347. Top K Frequent Elements
- 【LeetCode题解】203.Remove Linked List Elements
- Android子线程更新UI的两种方法
- Javascript知识点总结(三)
- codeforces 888E Maximum Subsequence
- 信号与系统杂七杂八的东西
- java异常处理
- cf886E Maximum Elements 题解
- IndicatorSeekBar 自定义SeekBar,滑动显示进度气泡指示器
- leetCode之Longest Substring Without Repeating Characters实现
- 十年程序人生__转自黎活明
- git 远程仓库和本地仓库建立连接
- 栈和队列的python实现
- React-Native之Android(6.0及以上)权限申请
- Lintcode72 Construct Binary Tree from Inorder and Postorder Traversal solution 题解
- 自定义视图之 重写函数 ondraw()