CF1463-D. Pairs
CF1463-D. Pairs
题意:
有从 1 1 1到 2 n 2n 2n一共 2 n 2n 2n个数字,让你将这 2 n 2n 2n个数字分成 n n n组,每组有两个数字。对于这 n n n组数字,你可以从中挑选 x x x组做 m i n min min操作,其他的 n − x n-x n−x组中做 m a x max max操作,这样就可以得到一个新的数组 b b b; 现在题目给你得到的数组 b b b,问你可以有多少不同的 x x x使得可以得到数组 b b b。
思路:
我们从这 2 n 2n 2n个数字中去掉数组 b b b中的数,剩下的就是数组 a a a中的数。对 a a a, b b b数组排序之后,我们现在先枚举每个 x x x让 a a a中最大的 x x x个数字从小到达与 b b b中最小的x个数字从小到大组合,让 a a a中剩余的 n − x n-x n−x个数字从小到大与 b b b中剩余的 n − x n-x n−x个数字从小到大组合(类似于贪心的思想),这个应该是最优的情况,如果这样还是不能通过前 x x x个取 m i n min min后 n − x n-x n−x取 m a x max max得到数组 b b b,那么对于这个 x x x无论你再怎么组合都不可能得到数组 b b b。
从理论上来说上面这种枚举+贪心的方法肯定能得到最终的答案,但是时间复杂度达到了 o ( n 2 ) o(n^2) o(n2),这是不能接受的。我们再仔细分析一下,会发现符合要求的 x x x是连续的、在一个区间里面的,原因如下:
我们假设符合要求的 x x x的区间为 [ L , R ] [L, R] [L,R]。现在我们将 x = R x=R x=R情况对应的组合进行操作可以得到 x = R + 1 x=R+1 x=R+1的情况:将数组 a a a中 n − x n-x n−x个最小的数字中最大的一个数字(称它为 i i i)与数组 b b b中 x x x个最小的数字中最小的一个数字(称它为 j j j)进行组合,这时候一定是因为 i < j i<j i<j从而取 m i n min min操作时不能得到 j j j所以不符合条件。而对于之后的 x = R + 1 , . . . , x = n x=R+1, ..., x=n x=R+1,...,x=n的情况, b b b中 x x x个数字最小的数字中最小的数字是不变的,而 a a a中 x x x个最小的数字是不断变小的,所以之后的情况也都是不符合的。同理我们也可以从 x = L − 1 , . . . , x = 0 x=L-1,..., x=0 x=L−1,...,x=0这些情况中得到同样的结论。
通过这个结论,我们可以通过两次二分查找,找到符合条件的 x x x区间 [ L , R ] [L, R] [L,R]的 L L L和 R R R,这样就可以将时间复杂度优化到 o ( n l o g n ) o(nlogn) o(nlogn)。
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>const int maxn = 2e5 + 5;int a[maxn], b[maxn];int check(int mid, int n) { // 0 suit; 1 l = mid + 1; 2 r = mid - 1;for (int i = 0; i < mid; i++) {if (b[i] > a[n - mid + i]) {return 2;}}for (int i = 0; i < n - mid; i++) {if (a[i] > b[mid + i]) {return 1;}}return 0;
}int main() {int T, n;scanf("%d", &T);while (T--) {scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &b[i]);}std::sort(b, b + n);int tot = 0, cur = 0;for (int i = 0; i < 2 * n; i++) {if (i + 1 == b[cur]) {cur++;} else {a[tot++] = i + 1;}}int l = 0, r = n;while (l <= r) {int mid = (l + r) >> 1;if (check(mid, n) != 1) {r = mid - 1;} else {l = mid + 1;}}int L = l;l = 0, r = n;while (l <= r) {int mid = (l + r) >> 1;if (check(mid, n) != 2) {l = mid + 1;} else {r = mid - 1;}}int R = r;printf("%d\n", R - L + 1);}return 0;
}
CF1463-D. Pairs相关推荐
- Pairs Forming LCM LightOJ - 1236
Pairs Forming LCM LightOJ - 1236 题意 问共有多少组数的最大公约数是n 分析 组合数学 ,唯一分解定理 参考代码 int Prime[670000]; const in ...
- 373. Find K Pairs with Smallest Sums (java,优先队列)
题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...
- Codeforces Round #562 (Div. 2) B. Pairs
链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...
- R语言散点图可视化:自定义标题和标签、拟合回归线、lowess为散点图添加平滑拟合线、修改散点图中点颜色和点符号、分组散点图、添加图例、pairs可视化散点图矩阵、ggplt2可视化、lattice
R语言散点图可视化:自定义标题和标签.拟合回归线.lowess为散点图添加平滑拟合线.修改散点图中点颜色和点符号.分组散点图.添加图例.pairs可视化散点图矩阵.ggplt2可视化.lattice ...
- 【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
题目如下: In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pair ...
- Quora Question Pairs 项目参考资料
实现多种解决方案的 kaggle比赛--Quora Question Pairs https://blog.csdn.net/qq_27009517/article/details/87716641? ...
- 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LUA表 pairs, ipairs输出顺序问题
LUA表 pairs, ipairs输出顺序问题 t = {[1] = 222,[2] = 23,[3] = 2433,[42] = 135,[5] = 1287,[7] = 7,[102] = 10 ...
- 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数
[抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...
最新文章
- python代码由CPU - >GPU
- java反序列化漏洞的一些gadget
- 键盘连击测试_测试梗欢迎补充
- mysql 6.5安装_RedHat6.5安装MySQL5.7教程详解
- python粘贴代码到word_Python复制Word内容并使用格式设字体与大小实例代码
- 操作系统(十九)进程互斥的软件实现方法
- 奇妙的数学动图,美到令人窒息!
- Python isinstance函数 - Python零基础入门教程
- requestAnimationFrame 方法你真的用对了吗?
- oracle中查找锁定状态的用户
- python dict setdefault_Python dict setdefault()用法及代码示例
- 1+X云计算平台运维与开发认证(初级)样卷E
- 【idea drools 执行20次失败中断崩溃】
- 乔布斯其人的演讲技巧
- 全职专业玩家分享:手动党梦幻五开赚钱心得
- norms matlab
- 怎么做SEO——页面权重计算公式
- 谷歌浏览器批量删除书签
- 【Python爬虫系列教程 41-100】猫眼电影字体加密破解
- windows下守护sqlserver进程并将bat注入服务