codeforces 493C Vasya and Basketball(枚举)

题目链接

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

题意:两个队打篮球赛,已知第一支球队进的每个球离篮筐的距离,第二支球队进的每个球离篮筐的距离。如果所进球离篮筐的距离小于等于d则得两分,大于d则得三分。现在d未知,输出一个可能的比分,使得第一支球队的得分与第二支球队的得分之差最大,若差最大的解不止一个,输出第一支球队最多得多少分?

题解:枚举d,枚举范围只用是所有两个球队所进球离篮筐的距离。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
#include<set>
#include<map>
#define nn 110000
#define inff 0x7fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n,m;
struct node
{
    int val;
    int id;
    node(){}
    node(int x,int y)
    {
        val=x,id=y;
    }
}a[4*nn];
bool cmp(node xx,node yy)
{
    return xx.val<yy.val;
}
int ans1,ans2,cha;
void solve(int x,int y)
{
    int ix=x*2+(n-x)*3;
    int fc=y*2+(m-y)*3;
    if(ix-fc>cha)
    {
        cha=ix-fc;
        ans1=ix,ans2=fc;
    }
    else if(ix-fc==cha)
    {
        if(ix>ans1)
        {
            ans1=ix,ans2=fc;
        }
    }
}
int main()
{
    int i,x;
    while(scanf("%d",&n)!=EOF)
    {
        int ix=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            a[++ix]=node(x,1);
        }
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d",&x);
            a[++ix]=node(x,2);
        }
        sort(a+1,a+ix+1,cmp);
        cha=-inff;
        int i1,i2;
        i1=i2=0;
        solve(i1,i2);
        a[ix+1]=node(-1,0);
        for(i=1;i<=ix;i++)
        {
            if(a[i].id==1)
            {
                i1++;
            }
            else
                i2++;
            if(a[i].val!=a[i+1].val)
            {
                solve(i1,i2);
            }
        }
        printf("%d:%d\n",ans1,ans2);
    }
    return 0;
}


你可能感兴趣的:(枚举,ACM,codeforces)