bzoj1034: [ZJOI2008]泡泡堂BNB

田忌赛马。

原理:(假设a队最大得分,得分为res)

1.如果a最小 > b最小,res += 2。 这样没必要用更大的a来解决这个b。

else 2.如果a最大 > b最小,res += 2。 这样没必要用更小的a解决这个b,这个b反正都要被解决,如果更小的a能解决这个b,那也能解决别的b。

else 3.上述两种情况不成立,就用a最小消耗b最大,b最大没人能打过,就用最弱的,a最小打不过别人,就拿它凑数。

这里要注意一点如果a最小 == b最大,res += 1。(为什么请读者自己体会)

最坏得分为2*n - b队得分。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100000 + 10;

int n;
int a[maxn],b[maxn];

int solve(int a[],int b[]) {
    int l1 = 0,l2 = 0,r1 = n-1,r2 = n-1,res = 0;
    while(l1 <= r1) {
        if(a[l1] > b[l2]) {
            res += 2;
            l1++;
            l2++;
        }
        else if(a[r1] > b[r2]) {
            res += 2;
            r1 --;
            r2 --;
        }
        else {
            res += (a[l1]==b[r2]);
            l1++;
            r2--;
        }
    }
    return res;
}
int main() {
    scanf("%d",&n);
    for(int i = 0; i < n; i++) scanf("%d",&a[i]);
    for(int i = 0; i < n; i++) scanf("%d",&b[i]);
    sort(a,a+n);
    sort(b,b+n);
    
    printf("%d %d\n",solve(a,b),2*n-solve(b,a));
    return 0;
}

posted @ 2016-04-12 23:26  invoid  阅读(124)  评论(0编辑  收藏  举报