题目364:田忌赛马

来源:互联网 发布:淘宝移动端优惠券 编辑:程序博客网 时间:2024/05/16 07:20

题目链接:

http://acm.nyist.net/JudgeOnline/problem.php?pid=364

描述

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

算法思想:

贪心算法,首先将速度进行从小到大进行预排序,比较n次,每次比较a[i] 与 b[j](a[i]代表田忌马第i大的速度,b[j]代表国王马第j大的速度)。
当a[i] > b[j]时,更新ans++,i++,j++;
当a[i] < b[j]时,则应该让a[i]与国王最快的马进行匹配,更新ans–,i++,len2–。
当a[i] == b[j]时,则判断a[len1]与a[len2],如果a[len1] > a[len2],则更新ans++,len1–,len2–;
如果a[len1] < a[len2],则应该让a[i]与国王最快的马进行匹配,更新ans–,i++,len2–。

源代码

#include <iostream>#include <algorithm>using namespace std;int n, *pint1, *pint2;int ans, num;int main(){    int ans, num;    while (cin >> n)    {        ans = 0;        num = 0;        pint1 = new int[n];        pint2 = new int[n];        for (int i = 0; i < n; i++)        {            cin >> pint1[i];        }        for (int i = 0; i < n; i++)        {            cin >> pint2[i];        }        //排序        sort(pint1,pint1 + n);        sort(pint2,pint2 + n);        int i, j, len1, len2;        i = j = 0;        len1 = len2 = n - 1;        while (n--)//比较n次        {            if (pint1[i] > pint2[j])//当田忌的马速度大于国王马速度,ans++            {                ans++;                i++;                j++;            }            else if (pint1[i] < pint2[j])            {                //当田忌的马的速度小于国王马的速度,则让该马与国王速度最大的马来比                ans--;                i++;                len2--;            }            //当当前马的速度一样            else            {                //比较速度最大的马的速度,如果田忌的大,则ans++,len1--,len2--                if (pint1[len1] > pint2[len2])                {                    ans++;                    len1--;                    len2--;                }                else                  {   //否则,说明没有比国王最大速度的马更快的马了,此时,让田忌最小速度的马与国王最快的马来比                    if (pint1[i] < pint2[len2])                    {                        ans--;                        i++;                        len2--;                    }                }            }        }        cout << 200 * ans << endl;    }    return 0;}

最优源代码

#include<stdio.h>#include<algorithm>using namespace std;int main(){    int a[1005],b[1005],i,a_first,a_last,b_first,b_last,n,A,B;    while(scanf("%d",&n)!=EOF)    {        A=0;B=0;        a_first=1; a_last=n;        b_first=1; b_last=n;        for(i=1;i<=n;i++)           scanf("%d",&a[i]);        for(i=1;i<=n;i++)           scanf("%d",&b[i]);        sort(a+1,a+n+1);        sort(b+1,b+n+1);        for(i=1;i<=n;i++)        {            if(a[a_first]>b[b_first])            {                a_first++;b_first++;A++;            }            else if(a[a_first]<b[b_first])            {                  b_last--;a_first++;B++;            }            else if(a[a_last]>b[b_last])            {                a_last--;b_last--;A++;            }            else if(a[a_last]<b[b_last])            {                 b_last--;a_first++;B++;            }            else if(a[a_first]<b[b_last])            {                 b_last--;a_first++;B++;            }        }        printf("%d\n",(A-B)*200);    }}

算法复杂度:

由源代码可知,算法时间复杂度O(n)。