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;
    }
  • 相关阅读:
    Median Value
    237. Delete Node in a Linked List
    206. Reverse Linked List
    160. Intersection of Two Linked Lists
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted Lists
    477. Total Hamming Distance
    421. Maximum XOR of Two Numbers in an Array
    397. Integer Replacement
    318. Maximum Product of Word Lengths
  • 原文地址:https://www.cnblogs.com/evenbao/p/9832215.html
Copyright © 2011-2022 走看看