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 ...
随机推荐
- 通过show profile分析sql语句
set profling=1; select count(*) from xuehao; show profiles; show profile for query 1; mysql> set ...
- Nginx(四):http服务器静态文件查找的实现
上一篇nginx的文章中,我们理解了整个http正向代理的运行流程原理,主要就是事件机制接入,header解析,body解析,然后遍历各种checker,直到处理成功为止. 我们以访问一个普通文件为例 ...
- 设计一款兼容ST207和GD207的开发板
在MCU的学习中,大部分人都是学习别人的开发板,例如正点原子.野火等,优点是有可靠的教程和代码,缺点是容易让人有种自己全部都学会的了错觉,听了课程编写了代码,运行正常. 这个时候,可以尝试自已做一块属 ...
- ftp设置二进制上传
一个不重要的数据库,备份是用expdp导出,然后上传到ftp服务器上面.上周这个主机宕机了,要在别的数据库恢复,发现报如下错误: ORA-39001: invalid argument value O ...
- SAPLink 非常好用的工具
对于SAP LINK,如果你想将一个程序完整的保存到本地,包括程序的自定义屏幕.菜单等等,那么请使用这个工具,它能够将一个程序完整的保存下来,并且移植到另一个SAP系统中,用来左程序的迁移和本地保存备 ...
- 与数论的厮守05:gcd(a,b)=gcd(b,a mod b)的证明
\[设c=gcd(a,b),那么a可以表示为mc,b可以表示为nc的形式.然后令a=kb+r,那么我们就\\ 只需要证明gcd(b,r)=c即可.{\because}r=a-kb=mc-knc,{\t ...
- Django Full Coverage
Django(个人推荐, 如果项目较大 需要协同开发, 建议使用django这种重量级框架, 如果类似于纯api的后端应用建议使用 flask, 轻量小巧 , 麻雀虽小五脏俱全) 1.Django是什 ...
- (Oracle)误删oracle表结构恢复
在操作数据库时,我们常常会不小心把表结构删除了.有时候建表很麻烦大到100多个字段,而又找不到当初的建表语句.其实这时候不用担心,oracle和咱们widows一样,他也有个回收站,只要你没有清除回收 ...
- 微服务中台落地 中台误区 当中台遇上DDD,我们该如何设计微服务
小结: 1. 微服务中台不是 /1堆砌技术组件就是中台 /2拥有服务治理就是中台 /3增加部分业务功能就是中台 /4Cloud Native 就是中台 https://mp.weixin.qq.com ...
- LOJ10069 TREE
题目描述 原题来自:2012 年国家集训队互测 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有 need 条白色边的生成树.题目保证有解. 输入格式 第一行 V,E,need 分 ...