Codeforces 493C. Vasya and Basketball【暴力+二分】
2016-09-22 16:36
295 查看
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 - b is maximum. If there are several such scores, find the one in which number
a is maximum.
Examples
Input
Output
Input
Output
题目大意:
给你A球队的n次投篮距离,给你B球队的m次投篮距离。让你自己定义一个三分线,然后使得A队得分与B队得分差值最大(如果有得分相同的情况,需要使A队得分最大)。
思路:
1、首先,三分线的枚举分成三种类型:
①设定三分线为0.
②设定三分线为无穷大.
③设定三分线为min(A球队最小射程,B球队最小射程)----max(A球队最大射程,B球队最大射程)之间的一个距离,其实要设在其之间,相当于设定在n+m个射程内的某一个。
2、那么我们暴力枚举n+m+2(这个+2是指上述第一第三种情况)个射程内的一个射程作为三分线,然后分别在排序过后的A数组中二分查找第一个大于等于这个三分线的位子记为posa,那么此时A球队得分为(n-posa)*3+posa*2,同理,找这样一个位子在B数组中,记为posb,那么B球队的得分为(m-posb)*3+posb*2,然后在这个过程中维护一个最大差值,最终输出最优解即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[200050];
int b[200050];
int c[400050];
int main()
{
int n,m,cont;
scanf("%d",&n);
cont=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
c[cont++]=a[i];
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
c[cont++]=b[i];
}
sort(a,a+n);
sort(b,b+m);
c[cont++]=0;
c[cont++]=2000000050;
int maxn=-0x3f3f3f3f;
int ansa=-1;
int ansb=-1;
for(int i=0;i<cont;i++)
{
int sanfen=c[i];
int l=0;
int r=n;
int posa=-1;
while(r>=l)
{
int mid=(l+r)/2;
if(a[mid]>=sanfen)
{
posa=mid;
r=mid-1;
}
else l=mid+1;
}
l=0;r=m;
int posb=-1;
while(r>=l)
{
int mid=(l+r)/2;
if(b[mid]>=sanfen)
{
posb=mid;
r=mid-1;
}
else l=mid+1;
}
int defena,defenb;
if(posa==-1)defena=n*2;
else defena=(n-posa)*3+posa*2;
if(posb==-1)defenb=m*2;
else defenb=(m-posb)*3+posb*2;
if(defena-defenb>maxn)
{
maxn=defena-defenb;
ansa=defena;
ansb=defenb;
}
else if(defena-defenb==maxn)
{
if(defena>ansa)
{
ansa=defena;
ansb=defenb;
}
}
}
printf("%d:%d\n",ansa,ansb);
}
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 - b is maximum. If there are several such scores, find the one in which number
a is maximum.
Examples
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球队的n次投篮距离,给你B球队的m次投篮距离。让你自己定义一个三分线,然后使得A队得分与B队得分差值最大(如果有得分相同的情况,需要使A队得分最大)。
思路:
1、首先,三分线的枚举分成三种类型:
①设定三分线为0.
②设定三分线为无穷大.
③设定三分线为min(A球队最小射程,B球队最小射程)----max(A球队最大射程,B球队最大射程)之间的一个距离,其实要设在其之间,相当于设定在n+m个射程内的某一个。
2、那么我们暴力枚举n+m+2(这个+2是指上述第一第三种情况)个射程内的一个射程作为三分线,然后分别在排序过后的A数组中二分查找第一个大于等于这个三分线的位子记为posa,那么此时A球队得分为(n-posa)*3+posa*2,同理,找这样一个位子在B数组中,记为posb,那么B球队的得分为(m-posb)*3+posb*2,然后在这个过程中维护一个最大差值,最终输出最优解即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[200050];
int b[200050];
int c[400050];
int main()
{
int n,m,cont;
scanf("%d",&n);
cont=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
c[cont++]=a[i];
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
c[cont++]=b[i];
}
sort(a,a+n);
sort(b,b+m);
c[cont++]=0;
c[cont++]=2000000050;
int maxn=-0x3f3f3f3f;
int ansa=-1;
int ansb=-1;
for(int i=0;i<cont;i++)
{
int sanfen=c[i];
int l=0;
int r=n;
int posa=-1;
while(r>=l)
{
int mid=(l+r)/2;
if(a[mid]>=sanfen)
{
posa=mid;
r=mid-1;
}
else l=mid+1;
}
l=0;r=m;
int posb=-1;
while(r>=l)
{
int mid=(l+r)/2;
if(b[mid]>=sanfen)
{
posb=mid;
r=mid-1;
}
else l=mid+1;
}
int defena,defenb;
if(posa==-1)defena=n*2;
else defena=(n-posa)*3+posa*2;
if(posb==-1)defenb=m*2;
else defenb=(m-posb)*3+posb*2;
if(defena-defenb>maxn)
{
maxn=defena-defenb;
ansa=defena;
ansb=defenb;
}
else if(defena-defenb==maxn)
{
if(defena>ansa)
{
ansa=defena;
ansb=defenb;
}
}
}
printf("%d:%d\n",ansa,ansb);
}
相关文章推荐
- CodeForces - 514D :R2D2 and Droid Army(二分、暴力)
- codeforces 650D D. Image Preview (暴力+二分+dp)
- 【打CF,学算法——二星级】CodeForces 626C Block Towers(暴力/二分)
- Codeforces 626E Simple Skewness(暴力枚举+二分)
- Codeforces - 831C. Jury Marks - 暴力+二分
- 牛客练习赛13 B 幸运数字Ⅱ 【暴力】【二分】
- codeforces 653D D. Delivery Bears(二分+网络流)
- CodeForces 363D Renting Bikes (二分+贪心)
- CodeForces--626C--Block Towers (二分)
- CodeForces 204A - Little Elephant and Interval 算答案..易错..暴力打表对拍
- 【CodeForces】779D - String Game(二分)
- CodeForces 128B - String 优先队列暴力..
- CodeForces 237C 筛素数+二分
- Codeforces 812C Sagheer and Nubian Market[二分]
- CodeForces 359D Pair of Numbers (暴力)
- Codeforces 363B Fence-暴力
- 【Codeforces 812 C. Sagheer and Nubian Market】+ 二分
- codeforces 814 C. An impassioned circulation of affection(暴力)
- Squares-暴力枚举或者二分
- codeforces 817C Really Big Numbers 二分