Vasya and Basketball codeforces 493c 二分二分

本文介绍了一种通过选择合适界限来最大化篮球比赛中第一队相对于第二队得分优势的算法。通过枚举并比较两队投篮距离,实现了得分差距的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Vasya and Basketball

Crawling in process...Crawling failedTime Limit:2000MS    Memory Limit:262144KB     64bit IO Format:%I64d & %I64u  

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 thand meters, where d is somenon-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 ofd. 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 follown integer numbers — the distances of throwsai (1 ≤ ai ≤ 2·109).

Then follows number m (1 ≤ m ≤ 2·105) — the number of the throws of the second team. Then followm integer numbers — the distances of throws ofbi (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 numbera is maximum.

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
题意:就是说给了两组数据,让你找到一个界限,使得第一组数的和与第二组数的差最大,的一组数的和是这样算的:不这个界限大的每个是三分,比这个界限小的每个是两份,对于这个数的话如果按两分算,那么所有与它相同的都是两分
思路:二分二分,我自己都炸了,做了好长时间,测试数据就一直错错,好不容易钱多少组过了,到38组超时了,顿时就不开心了,后来仔细的想想,看了别人博客介绍,就换了思路,果然直接ac,言归正传吧,刚开始的时候我是从两列数的最小的枚举到两列数的最大,这样就超时了,以为如果隔得数据相差太大,这样就算二分也会费很多时间如给1和999999999这样的话明显超时,后来改变思路,就是枚举第一个数列的每个数进行算差最大和枚举第二列数算差最大,在加上0和最大这两条界限的,得出来的就是最大差了。。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

int n;
int m;
long long a[300000];
long long b[300000];
long long inf = 0x7fffffff;
int main() {
    scanf("%d",&n);
    for(int i = 0; i < n; i++) {
        scanf("%I64d",&a[i]);
    }
    scanf("%d",&m);
    for(int i = 0; i < m; i++) {
        scanf("%I64d",&b[i]);
    }

    sort(a,a+n);
    sort(b,b+m);
    long long sum = n*3-m*3;
    long long aa=n*3,bb=m*3;

    for(long long i = 0; i < n; i++) {
        int k = lower_bound(a,a+n,a[i])-a;
        int g = lower_bound(b,b+m,a[i])-b;
        long long kk = 3*(n-k)+2*k;
        long long gg = 3*(m-g)+2*g;
        if(kk-gg > sum) {
            sum = kk-gg;
            aa=kk;
            bb=gg;
        }
        if(kk-gg == sum) {
            if(aa<kk) {
                aa=kk;
                bb=gg;
            }
        }
    }
    for(long long i = 0; i < m; i++) {
        int k = lower_bound(a,a+n,b[i])-a;
        int g = lower_bound(b,b+m,b[i])-b;
        long long kk = 3*(n-k)+2*k;
        long long gg = 3*(m-g)+2*g;
        if(kk-gg > sum) {
            sum = kk-gg;
            aa=kk;
            bb=gg;
        }
        if(kk-gg == sum) {
            if(aa<kk) {
                aa=kk;
                bb=gg;
            }
        }
    }
    if(n*2-m*2 > sum) {
        aa=n*2;
        bb=m*2;
    }
    if(n*2-m*2 == sum) {
        if(aa < n*2) {
            aa=n*2;
            bb=m*2;
        }
    }
    printf("%I64d:%I64d\n",aa,bb);
    return 0;
}



二分二分就是不好想,对于菜鸟简直是难做,但我们一定要坚持做下去,加油加油。。
### Codeforces Round 260 Div. 1 题目及题解 #### A. Vasya and Multisets 在这道题目中,Vasya有一个由n个整数组成的序列。目标是通过将这些数分成若干组,使得每组中的所有数都相同,并且尽可能减少分组的数量。 为了实现这一目的,可以利用贪心算法来解决这个问题。具体来说,在遍历输入数据的同时维护当前最大频率计数器,对于每一个新遇到的不同数值增加一个新的集合[^1]。 ```cpp #include <bits/stdc++..h> using namespace std; void solve() { int n; cin >> n; unordered_map<int, int> freq; for (int i = 0; i < n; ++i) { int x; cin >> x; freq[x]++; } int maxFreq = 0; for (auto& p : freq) { maxFreq = max(maxFreq, p.second); } cout << maxFreq << "\n"; } ``` #### B. Pashmak and Graph 此问题涉及图论领域的一个经典最短路径计算案例。给定一张带权无向图以及起点S和终点T,要求求出从S到T经过至少一条边后的最小花费总和。 Dijkstra算法适用于此类场景下的单源最短路径查询任务。初始化距离表dist[]为无穷大(INF),仅设置起始节点的距离为零;随后借助优先队列选取未访问过的最近邻接顶点u更新其相邻结点v至目前为止所知的最佳到达成本min{dist[u]+w(u,v)}直至找到终止条件即抵达目的地t或处理完毕所有可达区域内的候选者为止。 ```cpp typedef pair<long long,int> pli; const long long INF = LLONG_MAX / 3; struct Edge { int to, cost; }; vector<Edge> G[MAX_V]; long long d[MAX_V]; bool dijkstra(int s, int t){ priority_queue<pli,vector<pl>,greater<pl>> que; fill(d,d+MAX_V,INF); d[s]=0; que.push(pli(0,s)); while(!que.empty()){ pli p=que.top();que.pop(); int v=p.second; if(d[v]<p.first) continue; for(auto e:G[v]){ if(d[e.to]>d[v]+e.cost){ d[e.to]=d[v]+e.cost; que.push(pli(d[e.to],e.to)); } } } return d[t]!=INF; } ``` #### C. DZY Loves Colors 这是一道关于颜色染色的问题。给出长度为N的一维网格,初始状态下每个格子都有一个默认的颜色编号。现在有M次操作机会改变某些位置上的色彩值,最终目的是统计整个条带上共有几种不同的色调存在。 采用离散化技术预处理原始输入并记录下各段连续同色区间的端点坐标范围,之后针对每一次修改请求动态调整受影响部分的信息结构体(如线段树),最后依据累积的结果得出答案。 ```cpp // 假设已经实现了上述提到的数据结构 SegmentTree 和 update 函数 SegmentTree st; for(int i=1;i<=m;++i){ int l,r,c; scanf("%d%d%d",&l,&r,&c); update(l,r,c); } printf("%lld\n",st.query()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值