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的更多相关文章

  1. [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 ...

  2. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. 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 ...

  4. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  5. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  6. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  7. 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 ...

  8. 336-Palindrome Pairs

    336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...

  9. Palindrome Pairs

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  10. Calculating Stereo Pairs

    Calculating Stereo Pairs Written by Paul BourkeJuly 1999 Introduction The following discusses comput ...

随机推荐

  1. 更改mysql的密码

    mysql> set password for 'root'@'localhost' =PASSWORD('');Query OK, 0 rows affected (0.17 sec) mys ...

  2. 【Linux】关于CentOS系统中,文件权限第11位上是一个点的解读

    ------------------------------------------------------------------------------------------------- | ...

  3. mysql—if函数

    在mysql中if()函数的具体语法如下:IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值. 开始实 ...

  4. SVM 支持向量机算法-原理篇

    公号:码农充电站pro 主页:https://codeshellme.github.io 本篇来介绍SVM 算法,它的英文全称是 Support Vector Machine,中文翻译为支持向量机. ...

  5. Ubuntu Terminal命令行新建仓库并推送到远程仓库

    通常情况下,在本地新建一个仓库之后,需要在远端网页端也新建一个空的同名仓库,然后将两者进行关联才能推送. 那有没有办法直接在命令行就完成从新建到推送的过程而不需要中间在网页端也操作一番呢?办法当然是有 ...

  6. java.net.NoRouteToHostException: 没有到主机的路由

    今天在配置Jenkins 的云服务器的时候提示:java.net.NoRouteToHostException: 没有到主机的路由,网上查到的没有主机路由问题提到的大多是防火墙问题. 查看防火墙状态: ...

  7. Jenkins部署springboot项目

    记录jenkins如何部署springboot项目(jar类型的) 一.首先需要先配置好jenkins的基本配置(jdk.maven--),可在系统管理-->>全局工具配置中进行配置. 配 ...

  8. JavaScript基础知识-基本概念

    typeof操作符 typeof 操作符返回一个字符串,表示未经计算的操作数的类型. // 数值 typeof 37 === 'number'; typeof 3.14 === 'number'; t ...

  9. springboot配置rabbitmq

    一.消息生成者 1.1消息生成者配置 1.2 消息发送端代码 1.3 创建交换机,队列,并建立关系 二.消费者 2.1消费者 三.限流配置 3.1配置文件 #在单个请求中处理的消息个数,他应该大于等于 ...

  10. linux shell 条件判断if else, if elif else....

    在linux的shell中 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... ...