zoukankan      html  css  js  c++  java
  • [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
    ",ans1 , 2 * n - ans2);
        
        return 0;
    }
  • 相关阅读:
    php aes128加密
    redis应用场景
    Yii2开发小技巧
    yii2开启事务
    yii2验证密码->手机号码短信发送>手机短信发送频繁问题
    yii2 rules验证规则,ajax验证手机号码是否唯一
    随笔20170415
    v1版本
    数的划分
    单词接龙
  • 原文地址:https://www.cnblogs.com/evenbao/p/9832215.html
Copyright © 2011-2022 走看看