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. 【Java】面向对象

    重新搞一波 复习巩固 简单记录 慕课网 imooc Java 零基础入门-Java面向对象-面向对象 都是视频课件里的. 文章目录 面向对象 什么是对象 什么是面向对象 类 什么是对象的属性和方法 类 ...

  2. 【MySql】[ERROR] Can't read from messagefile '/usr/share/mysql/english/errmsg.sys'

    [root@zhang bin]# ./mysql_install_db --datadir=/usr/local/mysql/mydata/data/ 2018-08-18 03:09:14 [WA ...

  3. 【Oracle】权限相关

    系统权限 SELECT * FROM DBA_SYS_PRIVS WHERE GRANTEE = 'CHAXUN' UNION ALL SELECT * FROM DBA_SYS_PRIVS WHER ...

  4. kubernets之存活探针

    一   存活探针存在的意义 1.1  kubernet通过存活探针(liveness probe)检查容器是否还在运行,可以为pod中的每个容器单独指定存活探针,如果探针执行失败,kubernets会 ...

  5. 一个div画同心圆

    二话不说上代码 background-image:radial-gradient(7px,#00A4FF 50%,#fff 75%,#00A4FF 94%); 7px是圆的半径 效果:

  6. 牛逼!MySQL 8.0 中的索引可以隐藏了…

    MySQL 8.0 虽然发布很久了,但可能大家都停留在 5.7.x,甚至更老,其实 MySQL 8.0 新增了许多重磅新特性,比如栈长今天要介绍的 "隐藏索引" 或者 " ...

  7. 两种方式,花五分钟就能构建一个 Spring Boot 应用

    前言 Spring Boot 的好处自然不必多说,对于想要从事 Java 工作的朋友们来说,可谓是必学的技能. 在我看来,它的优势就是多快好省. 功能多,很多常用的能力都有集成: 接入快,简单的几行代 ...

  8. .net core 不同地区时间相互转换

    .net core 不同地区时间相互转换 //韩国时间转换成当前时间 //value=需要转换的时间 //Korea Standard Tim 韩国时间 //China Standard Time 中 ...

  9. 【链表】leetcode-1290-二进制链表转整数

    leetcode-1290-二进制链表转整数 题目描述 给你一个单链表的引用结点 head.链表中每个结点的值不是 0 就是 1.已知此链表是一个整数数字的二进制表示形式. 请你返回该链表所表示数字的 ...

  10. C/C++ Lua通信

    C/C++和Lua是如何进行通信的? http://www.luachina.cn/?post=38 2015-12-28 为了实现Lua和其他语言之间的通信,Lua虚拟机为C/C++提供了两个特性: ...