Hdu 1052 Tian Ji -- The Horse Racing

贪心算法。

贪心策略:

假设田忌的马为A,齐威王的马为B。要使价值最大化的话,则有如下几种情况:

1、使A中速度最次的马与B中速度最好的马比赛,使得B的价值损失最大。

2、使A中速度最好的马与B中速度最好的马比赛,使得B中价值损失最大。

3、如果A中速度最次的马与B中速度最次的马速度相等,则比较A中速度最大的马与B中速度最大的马的速度,有如下情况:

(1)若大于,根据2知须A中速度最大的马与B中速度最大的马比赛。

(2)若小于,则根据1知须A中最次的马与B中最好的马比赛。

 

接下来根据上面的贪心策略就得到了具体的方案: 

1、A最快 > B最快:即A的最快能打败B的所有队员,为了后面着想,必然跟B的最快比。

2、A最快 < B最快:即B的最快能打败A的所有队员,为了B对后面的输,所以B队最快那个就要浪费掉,拿去打A队最慢的。

3、A最慢 > B最慢:即B的最慢必然会输,A随便拿一个都赢他,当然拿最差的来赢他啦。

4、A最慢 < B最慢:即A的最慢必然会输,B随便拿一个都赢他,要B队浪费点,所以B队拿最快的和他比。

5、A最快 == B最快 && A最慢 == B最慢,最优是拿A最慢和B最快打,如许的话中心A队都能赢B队。

 

CODE:

 

#include <stdio.h>
#include <stdlib.h>
#include < string.h>
#include <algorithm>
using  namespace std;

const  int maxn =  1001;
int N;
int a[maxn], b[maxn];


int main()
{
     int i, tot;
     int firstA, firstB;
     int lastA, lastB;
     while(~scanf( " %d ", &N), N)
    {
        tot =  0;
        memset(a,  0sizeof(a));    
        memset(b,  0sizeof(b));
         for(i =  0; i < N; i++) scanf( " %d ", &a[i]);
         for(i =  0; i < N; i++) scanf( " %d ", &b[i]);
        sort(a, a+N);    sort(b, b+N);               // 从小到大排序 
        firstA = firstB =  0;                      
        lastA = lastB = N- 1;
         for(i =  0; i < N; i++) 
        {
             if(a[firstA] > b[firstB])       // A中最次大于B中最次 
            {
                tot++;
                firstA++;
                firstB++;
            }
             else  if(a[firstA] < b[firstB])  // A中最次小于B中最次 
            {
                tot--;
                firstA++;
                lastB--;
            }
             else  // A中最次等于B中最次 
            {
                 if(a[lastA] > b[lastB])    // A中最优大于B中最优 
                {
                    tot++;
                    lastA--;
                    lastB--;
                }
                 else  // A中最优小于或等于B中最优 
                {
                     if(a[firstA] < b[lastB])    // A中最次小于B中最优 
                    {
                        tot--;
                        firstA++;
                        lastB--;
                    }
                }
            }
        }
        printf( " %d\n ", tot* 200);
    }
     return  0;
}

 

你可能感兴趣的:(HDU)