Codeforces 493C - Vasya and Basketball
2 seconds
256 megabytes
standard input
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.
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 - b is 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
题目大意:找一个三分线d(大于d得3分,小于等于d得2分),使得第一支队伍得分减去第二只队伍得分(a-b)最大,并且输出a:b;如果有好几组答案,输出a最大的那组答案。
方法一:
将两只队伍的距离保存到一个数组c[]中,然后从c[]中枚举出三分线d,求出最大的(a-b),同时记录a和b。(在代码中x表示a,y表示b)
注意:注意边界,不然不能ac,下面以两个不同代码为例:
代码1:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int a[N],b[N],c[*N];
int main()
{
int n,m,cnt=;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
c[cnt++]=a[i];
}
cin>>m;
for(int i=;i<=m;i++)
{
cin>>b[i];
c[cnt++]=b[i];
}
sort(a+,a+n+);
sort(b+,b+m+);
sort(c+,c+cnt);
int ans ;
int x, y;
x = n * ;
y = m * ;
ans = x - y;//上边界(相当于代码2中的c[cnt-1]),是三分线d大于等于c[]最大值的情况,所以两队所有距离都只能得2分
for(int i=cnt-;i>=;i--)//所以这里从cnt-2开始,不过从cnt-1开始也可以ac
{//下边界是c[0]=0,比所有距离都小,所以两队所有距离都得3分
int temp1=upper_bound(a+,a+n+,c[i])-a-;
int temp2=upper_bound(b+,b+m+,c[i])-b-;
if(ans<=temp1*+(n-temp1)*-temp2*-(m-temp2)*)
{
x=temp1*+(n-temp1)*;
y=temp2*+(m-temp2)*;
ans=x-y;
}
}
cout<<x<<":"<<y<<endl;
return ;
}
代码2:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
const int INF=0x3f3f3f3f;
int a[N],b[N],c[*N];
int main()
{
int n,m,cnt=;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
c[cnt++]=a[i];
}
cin>>m;
for(int i=;i<=m;i++)
{
cin>>b[i];
c[cnt++]=b[i];
}
sort(a+,a+n+);
sort(b+,b+m+);
sort(c+,c+cnt);
int ans;
int x, y;
x = -INF;
y = INF;
ans = x - y; //初始值不赋值为上边界,赋值成负无穷
for(int i=cnt-;i>=;i--)//这里的上边界是c[cnt-1]
{//下边界是c[0]=0,比所有距离都小,所以两队所有距离都得3分
int temp1=upper_bound(a+,a+n+,c[i])-a-;
int temp2=upper_bound(b+,b+m+,c[i])-b-;
if(ans<=temp1*+(n-temp1)*-temp2*-(m-temp2)*)
{
x=temp1*+(n-temp1)*;
y=temp2*+(m-temp2)*;
ans=x-y;
}
}
cout<<x<<":"<<y<<endl;
return ;
}
方法二:与方法一差不多,不过是从a[]中枚举出三分线d,还有是用循环(而没有用upper_bound()这个函数)来找三分球个数。
代码3:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=+;
int a[N];
int b[N];
int main()
{
int n,m;
cin>>n;
for(int i=;i<n;i++)cin>>a[i];
cin>>m;
for(int i=;i<m;i++)cin>>b[i];
sort(a,a+n);
sort(b,b+m);
int j=m-;
int ab=n,ba=m;
int x=,y=;//x代表一队三分球个数,y代表二队三分球个数
for(int i=n-;i>=;i--)
{
while(j>=&&a[i]<=b[j])
{
j--;
y++;
}
x++;
if(x-y>=ab-ba)
{
ab=x;
ba=y;
}
}
if(ab<ba)ab=ba=;
cout<<n*+ab<<":"<<m*+ba<<endl;
return ;
}
Codeforces 493C - Vasya and Basketball的更多相关文章
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 暴力水题
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 排序
C. Vasya and Basketball Vasya follows a basketball game and marks the distances from which each te ...
- cf493C Vasya and Basketball
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- Vasya and Basketball CodeForces - 493C
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows ...
- 【Codeforces 493C】Vasya and Basketball
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举三分线(离散后)的位置 然后根据预处理的前缀和,快速算出两个队伍的分数. [代码] #include <bits/stdc++.h& ...
- codeforces 493 C Vasya and Basketball
题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能 ...
- Codeforces 837E. Vasya's Function
http://codeforces.com/problemset/problem/837/E 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...
随机推荐
- linux有趣的命令screen
screen类似一个容器, 可以把当前前台运行的应用shell窗口关闭而不影响运行, 跟后后nohup有点相似, 不过我觉得比nohup还好用 用法1: screen 然后会弹出一个新的shell窗口 ...
- 零基础Python爬虫实现(百度贴吧)
提示:本学习来自Ehco前辈的文章, 经过实现得出的笔记. 目标 http://tieba.baidu.com/f?kw=linux&ie=utf-8 网站结构 学习目标 由于是第一个实验性质 ...
- get与post需要注意的几点 (转)
get与post需要注意的几点 在面试或者笔试时,经常会被问到 HTTP 方法中 get 和 post 的异同点.本文简单整理归纳了一下,以备忘. 1."get/post" VS ...
- Thinkphp5 分页带参数
原文链接:http://www.zhaisui.com/article/51.html
- 分块读取Blob字段数据(Oracle)
试过了MSSQL的分块读取Blob字段,又尝试在Oracle下完成,发现还是可行的. 首先建立一个存储过程: create or replace procedure PRO_GET_BLOB( ...
- 关于linux下自定义的 alias文件和自定义函数库的通用写法(只适合自己的)
使用alias和自定义的function的必要性和重要性就不说了 , 自己的通用做法是: 可以创建: ~/bin/my.alias 文件 作为自定义的 alias专门文件 创建: ~/libsh/my ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群 最小点覆盖
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1741 思路 消除所有的小行星 每个点(x,y)只有选择x或者y才能被覆盖 二分图最小点覆盖= ...
- bzoj 2527: [Poi2011]Meteors
昨天写了一晚,越写复杂度越感觉不对,早上一想果然是假的. (这里n,m,k我就不区分了) 首先一个城市的询问可以很容易的二分 check用树状数组维护区间(区间修改,单点查询的那种) 一次是\(O(n ...
- 放棋子|2012年蓝桥杯B组题解析第七题-fishers
(13')放棋子 今有 6 x 6 的棋盘格.其中某些格子已经预先放好了棋子.现在要再放上去一些,使得:每行每列都正好有3颗棋子.我们希望推算出所有可能的放法.下面的代码就实现了这个功能. 初始数组中 ...
- hihoCoder week17 最近公共祖先·三 lca st表
记录dfs序列,dfn[tot] 记录第tot次访问的节点 然后查两点在dfs序中出现的第一次 id[u] id[v] 然后 找 dep[k] = min( dep[i] ) {i 属于 [id[u ...