zoukankan      html  css  js  c++  java
  • hdu 1052

    额。。。还就没写博客了,今天写一篇吧,1052这题刚开始看时,一下子就按匈牙利匹配算法那么写了。忘了数据量了。然后果断超时了,感觉好二逼啊,

    然后看了别人的博客,用贪心,卧槽,后来想一想我怎么这么二啊,这题正解本来就是贪心可做啊。摘抄一个简单的思路吧。

    思路:先把田忌和国王的马排序。
    
    
    每次取田忌的最快的马与国王最快的马比较,有三种情况。
    一,田忌最快的马比国王最快的快,那么直接拿田忌最快的马去赢国王最快的马。
    二,田忌最快的马比国王最快的慢,那么拿田忌最慢的马去输国王最快的马。
    三,田忌最快的马与国王最快的马速度一样。
          先拿田忌最慢的马与国王最慢的马比较。
          若田忌比国王快,直接赢掉国王最慢的马。
          否则田忌最慢的马再去与国王最快的马比。
         如果最快最慢的马都一样,用田忌最慢的马和国王最快的马比。
     
    代码如下:
    #include"stdio.h"
    #include"stdlib.h"
    
    int a[1005],b[1005],num[1005];
    
    int cmp(const void *a,const void *b)
    {
        int aa=*(int *)a,bb=*(int *)b;
        return bb-aa;
    }
    
    int main( )
    {
        int n,i,front,rear,head,tail,cnt;
        while(scanf("%d",&n)&&n!=0)
        {
            cnt=0;
            front=head=0;rear=tail=n-1;
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            for(i=0;i<n;i++)
                scanf("%d",&b[i]);
            qsort(a,n,sizeof(int),cmp);
            qsort(b,n,sizeof(int),cmp);
            while(front<=rear)
            {
                if(a[front]>b[head])
                {
                    num[front]=head;
                    front++;
                    head++;
                }
                else if(a[front]<b[head])
                {
                    num[rear]=head;
                    rear--;
                    head++;
                }
                else if(a[front]==b[head])
                {
                    if(a[rear]>b[tail])
                    {
                        num[rear]=tail;
                        rear--;
                        tail--;
                    }
                    else
                    {
                        num[rear]=head;
                        rear--;
                        head++;
                    }
                }
            }
            for(i=0;i<n;i++)
            {
                if(a[i]>b[num[i]])
                    cnt++;
                else if(a[i]<b[num[i]])
                    cnt--;
            }
            printf("%d\n",cnt*200);
        }
        return 0;
    }
     
  • 相关阅读:
    [Codeforces 1178D]Prime Graph (思维+数学)
    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)
    [HDU 3625]Examining the Rooms (第一类斯特林数)
    [Codeforces 364D]Ghd(随机算法+gcd)
    【快速幂】POJ3641
    【二分查找】POJ2456-Aggressive cows
    【判断解是否可行-二分】POJ1064-Cable master
    【动态规划/递推】BZOJ1806[IOI2007]- Miners
    【动态规划去除冗余】NOIP2010-乌龟棋
  • 原文地址:https://www.cnblogs.com/chaosheng/p/2908234.html
Copyright © 2011-2022 走看看