题目链接

题目

题目描述

Curling is a sport in which players slide stones on a sheet of ice toward a target area. The team with the nearest stone to the center of the target area wins the game.

Two teams, Red and Blue, are competing on the number axis. After the game there are \((n+m)\) stones remaining on the axis, \(n\) of them for the Red team and the other \(m\) of them for the Blue. The iii-th stone of the Red team is positioned at \(a_i\) and the \(i\)-th stone of the Blue team is positioned at \(b_i\).

Let \(c\) be the position of the center of the target area. From the description above we know that if there exists some \(i\) such that \(1 \le i \le n\) and for all \(1 \le j \le m\) we have\(|c - a_i| < |c - b_j|\) then Red wins the game. What's more, Red is declared to win \(p\) points if the number of \(i\) satisfying the constraint is exactly \(p\).

Given the positions of the stones for team Red and Blue, your task is to determine the position \(c\) of the center of the target area so that Red wins the game and scores as much as possible. Note that \(c\) can be any real number, not necessarily an integer.

输入描述

There are multiple test cases. The first line of the input contains an integer \(T\) indicating the number of test cases. For each test case:

The first line contains two integers nnn and mmm (\(1 \le n, m \le 10^5\)) indicating the number of stones for Red and the number of stones for Blue.

The second line contains nnn integers \(a_1, a_2, \cdots, a_n\) (\(1 \le a_i \le 10^9\)) indicating the positions of the stones for Red.

The third line contains mmm integers \(b_1, b_2, \cdots, b_m\) (\(1 \le b_i \le 10^9\)) indicating the positions of the stones for Blue.

It's guaranteed that neither the sum of nnn nor the sum of \(m\) will exceed \(5 \times 10^5\) .

输出描述

For each test case output one line. If there exists some \(c\) so that Red wins and scores as much as possible, output one integer indicating the maximum possible score of Red (NOT \(c\)). Otherwise output "Impossible" (without quotes) instead.

示例1

输入

3
2 2
2 3
1 4
6 5
2 5 3 7 1 7
3 4 3 1 10
1 1
7
7

输出

2
3
Impossible

备注

For the first sample test case we can assign \(c = 2.5\) so that the stones at position 2 and 3 for Red will score.

For the second sample test case we can assign \(c = 7\) so that the stones at position 5 and 7 for Red will score.

题解

知识点:STL,模拟。

选择一个点作为目标,如果存在红色石头比所有蓝色石头都严格接近目标,则红方胜利。进一步,满足比所有蓝色石头都严格接近目标的红色石头的数量是红方最后得分。

显然,红色石头会连成一段区间,中间不能有蓝色石头(包括端点),目标可以选在他们中间,使得这段红色石头都包括其中,并不含蓝色石头,最终得分就是连续红色石头的个数。地图上有很多蓝色石头分割了许多红色石头的区间,所以首先用一个 \(map\) 将坐标映射到红色石头数量,再将蓝色石头坐标的红色石头数量设为 \(0\) ,这样就有了所有石头的坐标,不为 \(0\) 的就是红色石头数量。遍历一遍,计算每个区间的红色石头个数,取其中最大值即可。

时间复杂度 \(O((n+m) \log (n+m))\)

空间复杂度 \(O(n+m)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n, m;
cin >> n >> m;
map<int, int> mp;
for (int i = 0, tmp;i < n;i++) {
cin >> tmp;
mp[tmp]++;
}
for (int i = 0, tmp;i < m;i++) {
cin >> tmp;
mp[tmp] = 0;
}
int ans = 0, sum = 0;
for (auto [i, j] : mp) {
if (j) sum += j;
else sum = 0;
ans = max(ans, sum);
}
if (ans) cout << ans << '\n';
else return false;
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "Impossible" << '\n';
}
return 0;
}

随机推荐

  1. Mysql 中 not in 的查询优化

    本文为博主原创,转载请注明出处: 最近做性能优化时,通过开启 MySQL 的慢日志查询配置,发现 有一条慢sql,在这里记录下分析和优化的过程. 该慢 sql 如下: select id from f ...

  2. Maven项目手动配置依赖项

    1.问题 很多时候,我们依靠其本身的识别功能,并不能很好的识别依赖项(尤其是指定版本),且对于一些位于 <\build>不能自动去下载,这时候我们就要去手动配置依赖项 2.解决 2.1 首 ...

  3. pybind11

    fatal error: Python.h: no such file or directory 在使用pybind11时,如果不做调整可能就会出现这样的情况,Python.h一般出现在usr/inc ...

  4. [转帖]字符集 AL32UTF8 和 UTF8

    https://blog.51cto.com/comtv/383254# 文章标签职场休闲字符集 AL32UTF8 和 UTF8文章分类数据库阅读数1992 The difference betwee ...

  5. [转帖]History of Unicode Release and Publication Dates

    www.unicode.org For ease of reference, this page collects together information about the dates for v ...

  6. Windows平台文件拆分与完整性检查的过程

    Windows平台文件拆分与完整性检查的过程 场景 有时候在没有linux主机的情况下, 自己下载下来的文件比较大. 比较难以上传到一些特殊的系统/主机上面. 这个时候需要将文件进行拆分. 所以可以通 ...

  7. [转帖]centos7离线安装postgresql13

    https://www.cnblogs.com/summer-88/p/15341918.html 在一台可以联网的centos上安装postgresql源 yum install -y https: ...

  8. [转帖]ebpf 月报 - 2023 年 1 月

    https://segmentfault.com/a/1190000043355631 本刊物旨在为中文用户提供及时.深入.有态度的 ebpf 资讯. 如果你吃了鸡蛋觉得好吃,还想认识下蛋的母鸡,欢迎 ...

  9. [转帖]AMD第四代宵龙 9174F 亮眼

    https://www.amd.com/zh-hans/processors/epyc-9004-series#%E8%A7%84%E6%A0%BC 型号规格   型号 CPU 核心数量 线程数量 最 ...

  10. 【Go WEB进阶实战】开源的电商前后台API系统

    前言 最近有很多小伙伴私信我:在学完Go基础后,想使用一个框架实战一个商业项目,但是又苦于不知道选择什么框架,更不知道做什么商业项目. 为了解决大家这些问题,我结合自己的项目经历,为大家开源了一个简单 ...