[bzoj1034][ZJOI2008]泡泡堂BNB【贪心】

【题目链接】
  http://www.lydsy.com/JudgeOnline/problem.php?id=1034
【题解】
  现将两个序列排好序。来考虑AB的最大收益。
  记la,ra,lb,lbA,b剩余数列的左右端点。
  若a[la]>b[lb]则选择这两个打(最小的打最小的)。
  若a[ra]>b[rb]则选择这两个打(最大的打最大的)。
  否则用a[la]去与b[rb]消耗。
  有一个点我一开始一直想不通,为什么a[la]=b[lb]时不选这两个打获得1分。
  解答是:因为当前a[ra]b[rb]所以b[rb]是无论如何不可能被击败的。那么为了让b[rb]的危害最小,那么一定是与a[la]打。YY一下感觉很有道理。
  

/* --------------
    user Vanisher
    problem bzoj-1034
----------------*/
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       100010
using namespace std;
int read(){
    int tmp=0, fh=1; char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
    return tmp*fh;
}
int a[N],b[N],n;
int solve(int *a, int *b){
    int pl1=1, pr1=n, pl2=1, pr2=n, num=0;
    while (pl1<=pr1){
        if (a[pl1]>b[pl2]){
            pl1++, pl2++, num+=2;
            continue;
        }
        if (a[pr1]>b[pr2]){
            pr1--, pr2--, num+=2;
            continue;
        }
        if (a[pl1]==b[pr2])
            num++;
        pl1++, pr2--;
    }
    return num;
}
int main(){
    n=read();
    for (int i=1; i<=n; i++) a[i]=read();
    for (int i=1; i<=n; i++) b[i]=read();
    sort(a+1,a+n+1); sort(b+1,b+n+1); 
    int ans1=solve(a,b), ans2=n*2-solve(b,a);
    printf("%d %d\n",ans1,ans2);
    return 0;
}
posted @ 2018-03-07 18:48  Vanisher  阅读(60)  评论(0编辑  收藏  举报