码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ1034

时间:2015-08-16 10:51:34      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:贪心

传送门:BZOJ1034

类似田忌赛马的贪心。
按照

我的最小能否赢过敌方最小?[1]
是:赢过,迭代
否:我的最大能否赢过敌方最大?[2]
是:赢过,迭代
否:我方最小比拼对方最大,迭代。

的流程进行。
让我们证明这个算法的正确性。当[1]成立时,最优性显然成立。当[1]不成立时,如我方最小输对方最小,正确性也是显然的。
在这里我们观察我方最小平敌方最小的情况,如进行比拼,收益为1,而比拼敌方最大后,我方最大能赢过敌方最小则收益为2,这种情况的唯一反例是我方最大能赢过敌方最大,在[2]中被叙述。
好吧我知道很不严谨……
代码上的小细节见下。

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

int n;
int me[100005],other[100005];

void Readdata()
{
    freopen("loli.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&me[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&other[i]);
    sort(me+1,me+n+1);
    sort(other+1,other+n+1);
}

int Solve(int* me ,int* other)
{
    int ans=0;
    int me_cl=1,me_op=n;
    int other_cl=1,other_op=n;
    for(int i=1;i<=n;i++)
        if(me[me_cl]>other[other_cl]){
            ans+=2;
            me_cl++;
            other_cl++;
        }
        else{
            if(me[me_op]>other[other_op]){
                ans+=2;
                me_op--;
                other_op--;
            }
            else{
                if(me[me_cl]==other[other_op])
                    ans++;
                me_cl++;
                other_op--;
            }
        }
    return ans;
}

void Close()
{
    fclose(stdin);
    fclose(stdout);
}

int main()
{
    Readdata();
    printf("%d ",Solve(me,other));
    printf("%d",2*n-Solve(other,me));
    Close();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

BZOJ1034

标签:贪心

原文地址:http://blog.csdn.net/le_ballon_rouge/article/details/47699857

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!