题目链接

题目

题目描述

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. VUEX 使用学习四 : action

    转载请注明出处: action 用于处理异步任务:action,可以操作任意的异步操作,类似于mutations,但是是替代mutations来进行异步操作的.首先mutations中必须是同步方法, ...

  2. docker 安装 ElasticSearch 和 Kibana 及ik 中文分词器

    本文为博主原创,未经允许不得转载: 1. 使用 docker 下载 elasticsearch  7.6.1 docker pull elasticsearch:7.6.1 2. 启动 elastic ...

  3. 03-点亮LED灯

    1.FPGA设计流程 1.设计规划 对项目需求了解,划分子功能模块,子功能模块的输入输出信号及通信关系 2.波形绘制 了解子模块的功能,画出框图,搞清楚如何通过输入信号得到输出信号,进而绘制波形图 3 ...

  4. FinalShell上传文件失败

    1.问题 上传文件失败,如图所示,即使切换至root用户 2.解决方式 这里在建立SSH连接时,就必须使用root用户,而若使用普通用户,即使在其中切换至root用户,也无法上传. 所以重新建立一个r ...

  5. 【转】嵌入式C语言代码优化方案

    来源:嵌入式C语言代码优化方案(深度好文,建议花时间研读并收藏) (qq.com) 1.选择合适的算法和数据结构 选择一种合适的数据结构很重要,如果在一堆随机存放的数中使用了大量的插入和删除指令,那使 ...

  6. 【面试题精讲】Redis如何实现分布式锁

    首发博客地址 系列文章地址 Redis 可以使用分布式锁来实现多个进程或多个线程之间的并发控制,以确保在给定时间内只有一个进程或线程可以访问临界资源.以下是一种使用 Redis 实现分布式锁的常见方法 ...

  7. PG数据库存储验证

    PG数据库存储验证 背景 最近学习了SQLServer数据库的varchar和nvarchar的存储 想到PG数据库其实没让选择字符集,也没有nvarchar 所以想学习一下nvarchar的使用情况 ...

  8. tempfs 的再学习

    tempfs 的再学习 背景 最近学习研究linux的内存buffer 和 cache相关的知识. 发现对linux的VFS的理解其实非常不到位. 再验证内存的使用的page caches和 drop ...

  9. 如何查看服务器的Raid缓存等配置的情况

    摘要 最近总遇到同一批机器的IO不一样的情况. 感觉可能跟硬件设备和Raid卡的设置不一样有关系. 所以今天学习研究了下storcli的命令. 希望能够进行一些数据的收集. Storcli简介 sto ...

  10. 冷备PG数据库并且直接使用Docker运行的方法

    PG数据库冷备以及使用Docker恢复运行的方法 总结: Docker运行命令 docker run -d --name postgres5433 --restart always -e POSTGR ...