BZOJ1034

傳送門: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;
}
發佈了54 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章