[ZJOI 2008] 泡泡堂BNB

[题目链接]

         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;
}

 

posted @ 2018-10-22 19:34  evenbao  阅读(147)  评论(0编辑  收藏  举报