冰壶是一项运动,运动员将冰面上的石头滑向目标区域。距离目标区域中心最近的石头的队伍赢得比赛。
红队和蓝队在数字轴上比赛。比赛结束后,轴线上还有(n+m)块石头,其中n块为红队,另外m块为蓝队。红队的第i块石头位于ai,蓝队的第i块石头位于bi。
设c为目标区域中心的位置。从上面的描述我们知道,如果存在一些i,使得1≤i≤n,并且对于所有1≤j≤m,我们有|c-ai|≤|c-bj|,那么Red赢得了游戏。更重要的是,如果满足约束的i的数量恰好为p,则Red被声明为赢得p点分数。
考虑到红队和蓝队的石头位置,你的任务是确定目标区域中心的位置c,以便红队赢得比赛并尽可能多地得分。请注意,c可以是任何实数,而不一定是整数。
输入
有多个测试用例。
输入的第一行包含一个整数 T 表示测试用例的数量。
对于每个测试用例:
第一行包含两个整数 n和 m(1 ≤ n,m ≤ 1e5)表示红色的石头数量和蓝色的石头数量。
第二行包含 n个整数 a1,a2 ,⋯, an (1 ≤ ai ≤ 1e9)指示红色的石头的位置。
第三行包含 m个整数 b 1 ,b 2 ,⋯,bm (1 ≤ bi ≤ 1e9),指示用于蓝色的宝石的位置。可以保证的 n 的总和与 m 的总和不超过 5e5.
输出
对于每个测试用例输出一行。
如果存在一些 c 使得Red获胜并且尽可能多地得分,输出一个整数,表示Red的最大可能得分 。否则输出“Impossible”(不带引号)。
Input
3
2 2
2 3
1 4
6 5
2 5 3 7 1 7
3 4 3 1 10
1 1
7
7
Output
2
3
Impossible
说明:
对于第一个样本测试用例,我们可以分配 c=2.5,使得红色的位置2和3处的石头将得分。
对于第二个样本测试用例,我们可以分配 c=7,使得位置5处的石头和位置7处的红色的石头将得分。
解析:
对于 |c - ai| < |c - bj|,即 ai 离 c 近,bj 相比较离 c 较远;
对于两个红队的石块,我们将 c 放在中间,这两个石块都能计数;
对于两个蓝队的石块,不能将 c 放在这两块石头之间,因为没有比这两块石头更近的红队石块;
这样问题就转换成求 最长连续的红队石块个数!
代码一:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef pair<int,int> PII;
const double PI=acos(-1.0);
const int N=2e6+10;
int t,n,m;
int a[N],b[N];
void solve()
{
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=m;i++) cin>>b[i];
sort (a+1,a+n+1);
sort (b+1,b+m+1);
int i=1,j=1;
int ans=0,cnt=0;
while (a[n]>b[m]) ans++,n--;
while (i<=n&&j<=m)
{
cnt=0;
while (i<=n&&j<=m&&a[i]>b[j]) j++;
while (i<=n&&a[i]<b[j]) cnt++,i++;
ans=max(ans,cnt);
while (i<=n&&j<=m&&a[i]==b[j]) i++; //此时 a和b中都有这个值,舍去
}
if (ans) cout<<ans<<endl;
else cout<<"Impossible\n";
}
signed main()
{
ios;
cin>>t;
while (t--) solve();
return 0;
}
代码二:(map)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef pair<int,int> PII;
const double PI=acos(-1.0);
const int N=2e6+10;
int t,n,m;
vector <int> a,b;
int ans,cnt;
map <int,int> k;
void solve()
{
cin>>n>>m;
a.clear(),b.clear(),k.clear();
ans=0,cnt=0;
for (int i=0;i<n;i++)
{
int x;
cin>>x;
a.push_back(x);
k[x]++;
}
for (int i=0;i<m;i++)
{
int x;
cin>>x;
b.push_back(x);
k[x]=-1;
}
for (auto l:k)
{
int x=l.first,y=l.second;
if (y==-1)
{
ans=max(ans,cnt);
cnt=0;
}
else cnt +=y;
}
ans=max(ans,cnt);
if (ans) cout<<ans<<endl;
else cout<<"Impossible\n";
}
signed main()
{
ios;
cin>>t;
while (t--) solve();
return 0;
}