[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=1034

[算法]

         考虑贪心

         首先将两个数组升序排序 

         若当前最弱的 > 对方当前最弱的,打

         若当前最强的 > 对方当前最强的,打

         否则用最弱的去打对方最强的

         时间复杂度 : O(NlogN)

[代码]

         

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
typedef long long LL;

int n;
LL a[MAXN] , b[MAXN];

template <typename T> inline void chkmax(T &x,T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
inline LL solve(LL *a,LL *b)
{
    LL ret = 0;
    int al = 1 , bl = 1 , ar = n , br = n;
    while (al <= ar && bl <= br)
    {
        if (a[al] > b[bl]) 
        {
            ret += 2;    
            ++al;
            ++bl;
        } else if (a[ar] > b[br])
        {
            ret += 2;
            --ar;
            --br;
        } else
        {
            if (a[al] == b[br]) ++ret;
            ++al; --br;
        }
    }    
    return ret;
}

int main()
{
    
    read(n);
    for (int i = 1; i <= n; i++) read(a[i]);
    for (int i = 1; i <= n; i++) read(b[i]);
    sort(a + 1,a + n + 1);
    sort(b + 1,b + n + 1);
    LL ans1 = solve(a , b) , ans2 = solve(b , a);
    printf("%lld %lld\n",ans1 , 2 * n - ans2);
    
    return 0;
}

 

相关文章:

  • 2021-12-16
  • 2021-12-09
  • 2021-07-12
  • 2022-02-04
  • 2021-05-31
  • 2022-01-27
  • 2021-07-16
猜你喜欢
  • 2021-04-27
  • 2021-07-25
  • 2021-10-05
  • 2021-11-04
  • 2021-12-15
  • 2021-09-01
  • 2021-06-06
相关资源
相似解决方案