题目:
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.
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).
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.
题意:在篮球比赛当中,给你n个距离和m个距离,问你如何设定三分线使得第一个人与第二个人的分数差最大,输出比分,如果分数差一样,选择第一个得分最高的那个人。
范围:1<=n,m<=200000 1<=距离<=20000000.
思路:其实思路比较简单,二分距离。用第一个人的得分去选择三分线,然后去二分得到第二个的得分。直接记录就可以,时间复杂度为nlogm,完全可行。
当然在这里借用了STL里的upper_bound();函数去查找第二个人根据三分线所得的分。用法自行补脑。
代码如下:
#include<cstdio> #include<cstring> #include<iostream> #include<sstream> #include<algorithm> #include<vector> #include<bitset> #include<set> #include<queue> #include<stack> #include<map> #include<cstdlib> #include<cmath> #define PI 2*asin(1.0) #define LL __int64 const int MOD = 1e9 + 7; const int N = 2e5 + 15; const int INF = (1 << 30) - 1; const int letter = 130; using namespace std; int n, m; int a[N], b[N]; int main() { while(cin >> n) { for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); cin >> m; for(int i = 0; i < m; i++) cin >> b[i]; sort(b,b+m); LL maxa = 2*n, maxb = 2*m; LL max1 = maxa - maxb; for(LL i = 0; i < n; i++) { LL ans = i * 2 + (n - i) * 3; LL vs = upper_bound(b, b + m, a[i] - 1) - b; ///重点,sort是基础 LL bns = vs * 2 + (m - vs) * 3; if((ans - bns) > max1 || (ans - bns == max1 && ans > maxa)) { max1 = ans - bns; maxa = ans, maxb = bns; } } printf("%I64d:%I64d\n", maxa, maxb); } return 0; }