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

本文提供了一种解决BZOJ-1034问题的有效算法,通过对比两个序列并采取最优策略来获取最大收益。讨论了不同情况下如何选择元素进行匹配,并附带实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值