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. 【计算机基础】常用的快捷键和DOS命令

    常用的快捷键和DOS命令 DOS命令 使用Linux比较酷 cool

  2. 【Sed】使用sed删除文件指定行的内容

    sed多看帮助文档,受益良多 sed -i '$d' filename 例如删除 /etc/profile的最后一行 cat -n /etc/profile ...    101  export PA ...

  3. 【Linux】nginx详细说明

    Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes 8; 工作进程:数目 ...

  4. ctfhub技能树—web前置技能—http协议—Cookie

    打开靶机环境 查看显示内容 根据提示,需要admin登录才能得到flag 题目介绍为Cookie欺骗.认证.伪造 介绍一下cookie和session 一.cookie: 在网站中,http请求是无状 ...

  5. 使用yaml来实现ingress-nginx

    创建一个ingress-nginx [root@k8s-master ingress]# cat ingress-nginx.yaml apiVersion: v1 kind: Namespace m ...

  6. migo的BAPI示例BAPI_GOODSMVT_CREATE

    1 *&---------------------------------------------------------------------* 2 *& Report Z_BAP ...

  7. Effective Java, 3e阅读笔记一

    引言 本书的目标是帮助读者更加有效地使用Java编程语言及其基本类库,适用于任何具有实际Java工作经验的程序员. 本书一共90个条目,12章,每个条目讨论一条规则,这些规则反映了最有经验的优秀程序员 ...

  8. MYSQL面试题-索引

    MYSQL面试题-索引 引自B站up编程不良人:https://www.bilibili.com/video/BV19y4y127h4 一.什么是索引? 官方定义:索引是一种帮助mysql提高查询效率 ...

  9. xtrabakcup基本用法 安装、全量备份恢复、增量备份恢复

    xtrabackup备份原理以及工作流程 备份流程日志分析:1.##读取mysql配置文件2.## 扫描innodb日志lsn并复制inndodb系统表空间3.## 缓冲写出到数据文件并锁表4.## ...

  10. uni-app开发经验分享二十: 微信小程序 授权登录 获取详细信息 获取手机号

    授权页面 因为微信小程序提供的 权限弹窗 只能通用户确认授权 所以可以 写一个授权页面,让用户点击 来获取用户相关信息 然后再配合后台就可以完成登录 <button class="bt ...