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.
3 1 2 3 2 5 6
9:6
5 6 7 8 9 10 5 1 2 3 4 5
15:10
解题思路:给出两个序列分别是选手1和选手2的投篮进球距离,求设定一个三分线,让比赛结果对选手1有利,输出比赛成绩。
枚举所有的三分线,原来枚举三分线的时候用了二分的思想,结果发现是错误的。还是老实枚举吧。
然后用二分来确定比赛的成绩是否对选手1更有利。(计算具体的比赛结果)
/********************************/
/*Problem: codeforces #493C# */
/*User: shinelin */
/*Memory: 3900 KB */
/*Time: 171 ms */
/*Language: GNU C++ */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x7fffffff
#define LL long long
vector<int> a;
vector<int> b;
set<int> c;
int n, m;
pair<int, int> dichSeach(int e)
{
int i = upper_bound(a.begin(), a.end(), e) - a.begin();
int p = upper_bound(b.begin(), b.end(), e) - b.begin();
return make_pair((n-i)*3 + i*2, p*2 + (m-p)*3);
}
int main()
{
int ax, bx;
scanf("%d", &n);
for (int i = 0; i < n; i ++)
{
scanf("%d", &ax);
a.push_back(ax);
c.insert(ax);
}
scanf("%d", &m);
for (int i = 0; i < m; i ++)
{
scanf("%d", &bx);
b.push_back(bx);
c.insert(bx);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
c.insert(0);
c.insert(a[n-1] > b[m-1] ? a[n-1] : b[m-1] + 1);
set<int>::iterator si = c.begin();
pair<int, int> pi = dichSeach(*si), Maxpi = pi;
int sub = pi.first - pi.second;
while (si != c.end())
{
pi = dichSeach(*si);
if(pi.first - pi.second > sub)
{
sub = pi.first - pi.second;
Maxpi = pi;
}
si ++;
}
printf("%d:%d", Maxpi.first, Maxpi.second);
return 0;
}
后来参考别人的想法,能不能只遍历a数组而不遍历b数组就得到最大的分差呢?当然直接遍历是有问题的。
举个例子:
3
3 8 14
3
6 11 12
直接遍历数组a的话,设8为三分线那么比分就是7:8,如果设定14为三分线,那么比分为6:6,而正确的比分应该是7:6.(设6或者7为三分线)。
但是我们可以这样做,设定比a数组小1的数为三分线,那么就可以解决这个问题。因为设定11和12为三分线,其实不如设定13为三分线,因为设定11为三分线的话,那么b可以多得一个12的三分,设定12和13为三分线的效果是等价的。这里用到贪心,意思就是如果选手1当前的距离di拿不到三分,那么我就让距离大点,让b也尽可能拿不到三分,前提是a的d(i+1)这个距离要拿到三分,所以索性设定三分线为d(i+1) -1。
要注意的是三分线可能超出两个数组的距离最大的一项,这时候大家都投两分球,这个随便取一个大的距离就可以了,笔者直接取INF。
/********************************/
/*Problem: codeforces #493C# */
/*User: shinelin */
/*Memory: 3100 KB */
/*Time: 93 ms */
/*Language: GNU C++ */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x7fffffff
#define LL long long
vector<int> a;
vector<int> b;
int n, m;
int getBScore(int e)
{
int p = upper_bound(b.begin(), b.end(), e) - b.begin();
return p * 2 + (m - p) * 3;
}
int main()
{
int ax, bx;
scanf("%d", &n);
for(int i = 0; i < n; i ++)
{
scanf("%d", &ax);
a.push_back(ax);
}
scanf("%d", &m);
for(int i = 0; i < m; i ++)
{
scanf("%d", &bx);
b.push_back(bx);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
//初始化的时候,三分线设置为0
int scoreB = m * 3;
int sub = n * 3 - m * 3;
int i = 0, ansA = n * 3, ansB = m * 3;
while(i <= n)
{
if(i > 0 && i < n && a[i] == a[i - 1])
{
i ++;
continue;
}
//i== n 设定一个较大的三分线。
if(i == n)
{
scoreB = getBScore(INF);
}
else
{
scoreB = getBScore(a[i] - 1);
}
if(n - i + n * 2 - scoreB > sub)
{
sub = n - i + n * 2 - scoreB;
ansA = n - i + n * 2;
ansB = scoreB;
}
i ++;
}
printf("%d:%d", ansA, ansB);
return 0;
}