ny 364 田忌赛马 && hdu 1052 Tian Ji -- The Horse Racing【贪心】

田忌赛马

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
Here is a famous story in Chinese history.

"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."

"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.
输入
The input consists of many test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
0


田忌赛马,贪心算法,先按从小到大排序,然后从最小的一端开始处理......


有几种情况:

1,如果自身当前最慢的比对方当前最慢的快,那么直接两个比掉,自己赢一局

2,如果自身当前最慢的比对方当前最慢的还要慢,那么没办法,只能拿自己最垃圾的去消耗掉对方最快的,反正这一匹马是绝对赢不了的别的马的

3,如果双方最慢的速度相同,那么就不能轻举妄动了,现在着手于他们双方最快的,如果自己最快的马比对方对方最快的马要快,那么直接两方最快的比,赢一局,

4,如果双方最慢的速度相同,而且自己的最快的马不比对方最快的马快,那就用自己最慢的消耗对方最快的。

一直选择到比赛结束。


这些贪心的策略是可以证明的,具体怎么样,最好自己想一想,最难想通是 3 ,为什么没有直接拿自己的最慢的去消耗掉对方最快的呢?

试想,如果现在自己最快的一匹马是 7 ,然后对方最快的两匹马分别是 5,6 ,那么对方的 6 被消耗掉,自己输一局之后,肯定会有一局,是自己的 7 对抗对方的 5 ,那么就无形中浪费了资源哦...


这样的赛马的贪心策略就是,尽量在自己比对方的马快的情况下,双方的速度差最小,这样才相当于在选择的时候选取的是最优的了...

具体为什么,多想想就明白了.........


#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[1005],b[1005];
void slove()
{
    sort(a,a+n);sort(b,b+n);
    int sum=0,ba=0,bb=0,ea=n-1,eb=n-1;
    while(ba<=ea)
    {
        if(a[ba]<b[bb])//自己最慢的比对方最慢的还慢 
        {
            sum-=200;//只能赔钱了.... 
            ++ba;--eb;//实在没办法,用自己最慢的消耗对方最快的 
        }
        else if(a[ba]>b[bb])//自己最慢的比对方最慢的快,直接比 
        {
            sum+=200;//赚钱了 
            ++ba;++bb; 
        }
        else//最慢的相同,就要看最快的谁快了........ 
        {
            if(a[ea]<=b[eb])//如果自己最快的没有对方最快的快 
            {
                if(a[ba]<b[eb])//看自己最慢的是否比对方最快的慢 
                {
                    sum-=200;
                }
                ++ba;--eb;//用自己最慢的去消耗对方最快的 
            }
            else//自己最快的比对方最快的还要快,直接比 
            {
            	sum+=200; 
                --ea;--eb;//那就打平一局, 
            }
        }
    }
    printf("%d\n",sum);
}

int main()
{
    //freopen("shuju.txt","r",stdin);
    while(~scanf("%d",&n))
    //  hdu格式  while(scanf("%d",&n),n)
    {
        for(int i=0;i<n;++i)
        {
            scanf("%d",a+i);
        }
        for(int i=0;i<n;++i)
        {
            scanf("%d",b+i);
        }
        slove();
    }
    return 0;
}





你可能感兴趣的:(ny 364 田忌赛马 && hdu 1052 Tian Ji -- The Horse Racing【贪心】)