CF_493 C- Vasya and Basketball(二分)

C. Vasya and Basketball
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.

Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — the number of throws of the first team. Then follow n integer numbers — the distances of throws ai (1 ≤ ai ≤ 2·109).

Then follows number m (1 ≤ m ≤ 2·105) — the number of the throws of the second team. Then follow m integer numbers — the distances of throws of bi (1 ≤ bi ≤ 2·109).

Output

Print two numbers in the format a:b — the score that is possible considering the problem conditions where the result of subtractiona - b is maximum. If there are several such scores, find the one in which number a is maximum.

Sample test(s)
input
3
1 2 3
2
5 6
output
9:6
input
5
6 7 8 9 10
5
1 2 3 4 5
output
15:10

题意:

题目输入两组数,找出最合适的三分线设置位置使一队与二队的分差最大(赢得最多或者输得最少)。

题解:

最先想到的一定是枚举三分线位置找到最大分差,但是仔细思考后发现只需要遍历出现过的投球距离就可以,确切的说是只需要遍历a的投篮距离就好。枚举三分线位置,计算得分时采用二分查找定位当前三分线位置,之后计算得分就很简单了。时间复杂度控制在O(nlogn)之内。

关于投篮距离便利的解释:

因为题目要求不超过距离d的都记为两分,超过距离d才记为3分。最开始是想将a,b投篮距离放在一起遍历,但是后来想到其实只需要遍历a的投篮距离,每次遍历的距离设置为a[i]-1,这样可以保证在两个距离区间内无论b怎么取得分始终是最少的,而起a的得分求起来快了很多。最后再在两端取极限值更新一下,时间和空间都比原来的快了一半。

代码实现:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cstring>
#define MAX 200005
#define INF 0xffffff


using namespace std;


int a[MAX];//team1
int b[MAX];//team2
int n,m;
int score[2];//储存得分
void solve();
bool if_win(int);
int main()
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    //初始化最大
    score[0]=0;score[1]=INF;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=0;i<m;i++)
        scanf("%d",&b[i]);
    //将三个数组排序
    sort(a,a+n);
    sort(b,b+m);
    solve();
    return 0;
}
void solve()
{
    //遍历
    for(int i=0;i<n;i++)
    {
        //更新
        if(i!=0&&a[i]==a[i-1])
            continue;
        if_win(i);
    }
    //最后再将左右边界比较一下
    a[0]=0;
    if_win(0);
    a[0]=0xffffff;
    if_win(0);
    printf("%d:%d\n",score[0],score[1]);
}
bool if_win(int x)
{
    int y=a[x]-1;
    //s1,s2储存得分,记为long long防止越界
    long long s1,s2;
    s1=s2=0;
    //计算a的得分
    if(x!=0)
        s1=x*2+(n-x)*3;
    else if(x==0&&a[x]==0xffffff)
        s1=n*2;
    else
        s1=n*3;
    //用二分计算b的得分
    if(b[0]>y)
        s2=m*3;
    else if(b[m-1]<=y)
        s2=m*2;
    else
    {
        double lb=0,ub=2*(m-1);
        for(int i=0;i<100;i++)
        {
            int mid=(int)((lb+ub)/2);
            if(b[mid]<=y&&b[mid+1]>y)
            {
                s2+=(mid+1)*2;
                s2+=(m-1-mid)*3;
                break;
            }
            else
            {
                if(b[mid]<=y)
                    lb=mid;
                else
                    ub=mid;
            }
            if(lb==ub)
                break;
        }
    }
    //更新得分差
    if(s1-s2>score[0]-score[1])
    {
        score[0]=s1;
        score[1]=s2;
    }
    //依题意相等的话找出a得分较高的那一组
    else if(s1-s2==score[0]-score[1])
    {
        if(s1>score[0])
        {
            score[0]=s1;
            score[1]=s2;
        }
    }
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值