CF1463-D. Pairs
CF1463-D. Pairs
题意:
有从\(1\)到\(2n\)一共\(2n\)个数字,让你将这\(2n\)个数字分成\(n\)组,每组有两个数字。对于这\(n\)组数字,你可以从中挑选\(x\)组做\(min\)操作,其他的\(n-x\)组中做\(max\)操作,这样就可以得到一个新的数组\(b\); 现在题目给你得到的数组\(b\),问你可以有多少不同的\(x\)使得可以得到数组\(b\)。
思路:
我们从这\(2n\)个数字中去掉数组\(b\)中的数,剩下的就是数组\(a\)中的数。对\(a\),\(b\)数组排序之后,我们现在先枚举每个\(x\)让\(a\)中最大的\(x\)个数字从小到达与\(b\)中最小的x个数字从小到大组合,让\(a\)中剩余的\(n-x\)个数字从小到大与\(b\)中剩余的\(n-x\)个数字从小到大组合(类似于贪心的思想),这个应该是最优的情况,如果这样还是不能通过前\(x\)个取\(min\)后\(n-x\)取\(max\)得到数组\(b\),那么对于这个\(x\)无论你再怎么组合都不可能得到数组\(b\)。
从理论上来说上面这种枚举+贪心的方法肯定能得到最终的答案,但是时间复杂度达到了\(o(n^2)\),这是不能接受的。我们再仔细分析一下,会发现符合要求的\(x\)是连续的、在一个区间里面的,原因如下:
我们假设符合要求的\(x\)的区间为\([L, R]\)。现在我们将\(x=R\)情况对应的组合进行操作可以得到\(x=R+1\)的情况:将数组\(a\)中\(n-x\)个最小的数字中最大的一个数字(称它为\(i\))与数组\(b\)中\(x\)个最小的数字中最小的一个数字(称它为\(j\))进行组合,这时候一定是因为\(i<j\)从而取\(min\)操作时不能得到\(j\)所以不符合条件。而对于之后的\(x=R+1, ..., x=n\)的情况,\(b\)中\(x\)个数字最小的数字中最小的数字是不变的,而\(a\)中\(x\)个最小的数字是不断变小的,所以之后的情况也都是不符合的。同理我们也可以从\(x=L-1,..., x=0\)这些情况中得到同样的结论。
通过这个结论,我们可以通过两次二分查找,找到符合条件的\(x\)区间\([L, R]\)的\(L\)和\(R\),这样就可以将时间复杂度优化到\(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的更多相关文章
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 数论 - Pairs(数字对)
In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 336-Palindrome Pairs
336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...
- Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- Calculating Stereo Pairs
Calculating Stereo Pairs Written by Paul BourkeJuly 1999 Introduction The following discusses comput ...
随机推荐
- 【Docker】Failed to get D-Bus connection: Operation not permitted解决
------------------------------------------------------------------------------------------------- | ...
- MCU的心脏-晶振
晶振是石英晶体谐振器(quartzcrystal oscillator)的简称,它被称为电路系统的心脏,它为整个系统提供"心跳".中央处理器(CPU)一切指令的执行都是建立在这个& ...
- logging模块简单用法
logging模块功能比较多,但一般情况下使用其简单功能就已经足够了. 最简单的用法如下: import logging logging.baiscConfig(level=logging.DEBUG ...
- linux自定义安装位置安装jdk
注:本文系参考网络内容及本人实践得出 1 下载jdk安装包 下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downl ...
- using-pointers-to-remove-item-from-singly-linked-list
https://stackoverflow.com/questions/12914917/using-pointers-to-remove-item-from-singly-linked-list
- 1、剑指offer-数组——二维数组中的查找
*题目描述* **在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含 ...
- What is :: (double colon) in Python when subscripting sequences?
What is :: (double colon) in Python when subscripting sequences? 15 Extended Slices https://docs.pyt ...
- gstack pstack strace
gstack pstack strace 通过进程号 查看 进程的工作目录 Linux神器strace的使用方法及实践 - 知乎 https://zhuanlan.zhihu.com/p/180053 ...
- WPF入门学习(转)
WPF基础知识 总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽然WPF是XAML配置的,但是总还是要写代码的,相信各位读者应该也都有这个基础 ...
- Trie 前缀树或字典树 确定有限状态自动机
https://zh.wikipedia.org/wiki/Trie 应用 trie树常用于搜索提示.如当输入一个网址,可以自动搜索出可能的选择.当没有完全匹配的搜索结果,可以返回前缀最相似的可能.[ ...