CodeForces 493C 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 subtraction a - bis 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

题意:

两个人比赛投篮。输入n,代表A的投中数量为n个;接着输入n个A的得分点,也就是距离。同样,输入m,代表B的投中数量为m个;接着输入m个B的得分点。三分线的距离是d,目标是选取一个合适的三分线位置,使得scorea - scoreb的值最大。


题解:

这道题可以使用穷举法解,穷举三分线d在A的每一个得分点之前1的位置,即A的某个得分点是7,则穷举到此处得分点时,三分线d设置为6,这样就能保证7是第一个三分开始点,然后计算三分线d = 6时A的得分与B的得分差。将A的元素穷举完之后,就会得到一个最优解。

例: A:3 6 8 12

d : 2 5 7 11

为什么对A数组进行遍历呢?

我原先是对这两个数组都进行遍历,三分线在这两个数组中找,但是后来想想对B数组进行遍历其实是多余的,因为就A数组来说,每个得分点只有两种可能性,两分和三分,当遍历A数组时,每个得分点的可能性都已经被遍历过了,因此再遍历B数组其实也只是重复操作。

为什么对B数组进行遍历呢?

我试过对B数组进行遍历,将B数组的每个值减去1当做三分线位置,然后计算结果,提交上去后WA了。

测试点:

3
3 4 9
2
1 10

就这个测试点来说,B数组为:1,10, 则三分线d:0, 9;则结果最大为 9:6,答案为9:5.错误的原因在于,A数组中的状态没有完全遍历到,如果将d设置为2或者3,就会得到9:5的答案。因此,不能只对B数组进行遍历。

综上:不能对B数组进行遍历,只需要对A数组进行遍历,最后还需要考虑一下两个边界点,代码如下。

#include <iostream>
#include <string.h>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cmath>
#include <math.h>
#include <vector>
#include <map>
#include <time.h>
#include <ctime>
using namespace std;
#define M 200005
int num1[M];    //玩家A的得分点
int num2[M];    //玩家B的得分点
int main()
{
    int n, m;
    memset(num1,0,sizeof(num1));
    memset(num2,0,sizeof(num2));
    cin >> n;                           //输入
    for(int i = 0; i < n; i++)
        scanf("%d",&num1[i]);
    cin >> m;
    for(int i = 0; i < m; i++)
        scanf("%d",&num2[i]);
    sort(num1,num1+n);          //以递增方式排序
    sort(num2,num2+m);
    int scorea, scoreb;             //用于记录A的得分和B的得分
    int max = -0xfffffff;
    int maxi1 = 0;              //用于保存A和B的最大值,最后输出
    int maxi2 = 0;
    int dis;
    int flag = 0;
    int i, j;
    int tmp;
    /*这里只需要遍历一遍A数组,不需要再遍历一遍B数组,因为A的最大值如果*/
    for(i = 0; i <= n; i++)         //遍历A数组
    {
        if(i == n)
            dis = num1[n-1];
        else
            dis = num1[i]- 1;       //这里减1的目的在于将三分线画在num[i]-1处,则num[i]这个点的得分就是三分
        scorea = 0;
        scoreb = 0;

        tmp = upper_bound(num1,num1+n,dis) - num1;          //找到第一个大于三分线的点,从这个点开始之后的点得分都是三分
        scorea = (n-tmp)*3 + tmp*2;
        tmp = upper_bound(num2,num2+m,dis) - num2;          //和上面一样,这个是B数组的得分
        scoreb = (m-tmp)*3 + tmp*2;
        if((scorea - scoreb) > max)             //进行比较,取最大的值
        {
            max = scorea - scoreb;
            maxi1 = scorea;
            maxi2 = scoreb;
        }
    }
    if((n*2 - m*2) > max)               //边界处理,此处三分线比所有的得分点的值都大,因此得分都为两分
    {
        maxi1 = n*2;
        maxi2 = m*2;
    }
    if(maxi1 == maxi2)                  //根据题意,如果他们两个一样,则让他们的每个得分点都取三分
        printf("%d:%d",n*3,m*3);
    else
        printf("%d:%d\n",maxi1,maxi2);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值