题目链接

题目

题目描述

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. 万字血书Vue—走近Vue

    Vue是什么? Vue是一套用于构建用户界面的渐进式JavaScript框架 构建用户界面:用vue往html页面中填充数据 渐进式:Vue可以自底向上逐层的应用,从轻量小巧核心库的简单应用,到引入各 ...

  2. 类外static函数定义要不要加static关键字?

    类外static函数定义要不要加static关键字? 先说答案:不需要. 错误代码: #include<iostream> #include<memory> using nam ...

  3. [转帖]一份完整的阿里云 Redis 开发规范,值得收藏!

    https://blog.csdn.net/NicolasLearner/article/details/117449847 作者:付磊-起扬 http://yq.aliyun.com/article ...

  4. [转帖]oracle 11.2.0.4 rac集群等待事件enq: TM - contention

    近期,一金融客户oracle 11.2.0.4 rac集群delete不当导致等待事件enq: TM - contention严重引起大范围会话堆积,记录的相关分析工作如下. 1.登录集群任意节点,查 ...

  5. [转帖]12.计算机网络---iptables防火墙管理工具

    文章目录 一.防火墙基础知识 1.1 防火墙是什么? 1.2 iptables基础知识 1.3 netfilter和iptables的关系: 1.4 新型防火墙工具:firewalld 二.iptab ...

  6. [转帖]LTP使用和分析

    一.安装及编译流程 1.下载LTP LTP 项目目前位于 GitHub,项目地址:https://github.com/linux-test-project/ltp . 获取最新版可以执行以下命令: ...

  7. Springboot数据库连接池的学习与了解

    背景 昨天学习总结了tomcat的http连接池和线程池相关的知识,总结的不是很完整, 自己知道的也比较少,总结的时候就在想tomcat针对client 端有连接池,并且通过NIO的机制, 以较少的t ...

  8. 大数据平台Bug Bash大扫除最佳实践

    一.背景 随着越来越多的"新人"在日常工作以及大促备战中担当大任,我们发现仅了解自身系统业务已不能满足日常系统开发运维需求.为此,大数据平台部门组织了一次Bug Bash活动,既能 ...

  9. elementUI自定义单选框内容

    <template> <div> <div class="heng-div"> <el-radio v-model="radio ...

  10. PKI系统

    PKI系统简介 PKI(Public Key Infrastructure,公钥基础设施)是一种密码学框架,用于安全地管理数字证书.公钥和私钥,以确保通信和数据的机密性.完整性和身份验证.PKI建立在 ...