牛客竞赛算法入门题单打卡 N Let‘sPlayCurling

题意:

有t组输入,每组输入中有一个大小为n的数组a和一个大小为m的数组b,现在有任意的一点c,对于a组,如果ai对于所有的bj满足|c - ai| < |c - bj|,那么a队得一分,现在要找到一点c,让a队获得尽可能多的分数,并将分数输出,如果不能得分,则输出Impossible.

题目描述

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)(n+m)(n+m) stones remaining on the axis, nnn of them for the Red team and the other mmm of them for the Blue. The iii-th stone of the Red team is positioned at aia_iai​ and the iii-th stone of the Blue team is positioned at bib_ibi​.

Let ccc be the position of the center of the target area. From the description above we know that if there exists some iii such that 1≤i≤n1 \le i \le n1≤i≤n and for all 1≤j≤m1 \le j \le m1≤j≤m we have ∣c−ai∣<∣c−bj∣ then Red wins the game. What's more, Red is declared to win ppp points if the number of iii satisfying the constraint is exactly ppp.

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

输入描述:

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

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

The second line contains nnn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_na1​,a2​,⋯,an​ (1≤ai≤1091 \le a_i \le 10^91≤ai​≤109) indicating the positions of the stones for Red.

The third line contains mmm integers b1,b2,⋯ ,bmb_1, b_2, \cdots, b_mb1​,b2​,⋯,bm​ (1≤bi≤1091 \le b_i \le 10^91≤bi​≤109) indicating the positions of the stones for Blue.

It's guaranteed that neither the sum of nnn nor the sum of mmm will exceed 5×1055 \times 10^55×105.

输出描述:

For each test case output one line. If there exists some ccc so that Red wins and scores as much as possible, output one integer indicating the maximum possible score of Red (NOT ccc). 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.5c = 2.5c=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=7c = 7c=7 so that the stones at position 5 and 7 for Red will score.

思路

不需要找出具体c的位置,而是看每个bi到bi+1之间有几个a中元素

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		int a[100010],b[100010];
		cin>>n>>m;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=1;i<=m;i++)cin>>b[i];
		sort(a,a+n);
		sort(b+1,b+m+1);
		b[0]=0;b[m+1]=1e9+1;
		int ans=0;
		for(int i=0;i<=m;i++)
		{
			int pl=upper_bound(a,a+n,b[i])-a;
			int pr=lower_bound(a,a+n,b[i+1])-a;
			ans=max(ans,pr-pl);
		}
		if(ans==0){
			cout<<"Impossible"<<endl;
		}
		else
		cout<<ans<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值