其他分享
首页 > 其他分享> > 杭电1052(贪心)

杭电1052(贪心)

作者:互联网

1.贪心策略:能赢就赢,不能赢就用最坏的马消耗最好的马。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 1010;
int a[INF];
int b[INF];
int vis[INF];
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int main()
{
    int ans=0;
    int n;
    int tlow,klow,thi,khi;
    while(cin>>n&&n)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
        }
        qsort(a,n,sizeof(a[0]),cmp);
        qsort(b,n,sizeof(b[0]),cmp);
        tlow=0;
        klow=0;
        thi=n-1;
        khi=n-1;
        for(int i=0;i<n;i++)
        {
            if(a[tlow]>b[klow])
            {
                ans++;
                tlow++;
                klow++;
            }
            else if(a[tlow]<b[klow])
            {
                ans--;
                tlow++;
                khi--;
            }
            else
            {
                if(a[thi]<b[khi])
                {
                    ans--;
                    tlow++;
                    khi--;
                }
                else if(a[thi]>b[khi])
                {
                    ans++;
                    thi--;
                    khi--;
                }
                else
                {
                    if(a[tlow]<b[khi])
                    {
                        ans--;
                    }
                        tlow++;
                        khi--;
                }
            }
        }
        cout<<ans*200<<endl;
    }
    return 0;
}

标签:1052,khi,int,++,tlow,杭电,--,ans,贪心
来源: https://blog.csdn.net/mingjiweixiao/article/details/115218390