HDU1052 Tian Ji -- The Horse Racing(贪心)

来源:互联网 发布:windows禁止启动程序 编辑:程序博客网 时间:2024/05/22 09:42

题目:

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30306    Accepted Submission(s): 9150


Problem Description
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.
 

Input
The input consists of up to 50 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. The input ends with a line that has a single 0 after the last test case.
 

Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
 

Sample Input
392 83 7195 87 74220 2020 20220 1922 180
 

Sample Output
20000
 

Source
2004 Asia Regional Shanghai
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  2037 1009 1045 1800 1257 
 
思路:

先说题意:经典的田忌赛马问题,题目先给了一个n代表田忌和国王的马的数量,然后分两行给出了田忌的马和国王的马的速度,每一局的筹码是200块钱,问田忌最多能获得多少钱

我们采用贪心的思想,使局势对于田忌更有利

1.给田忌和国王的马的速度从小到大排序(速度小的马在前面,速度快的马在后面)

2.定义两个指针指向田忌的慢马和快马(tl,tr),国王的慢马和快马(ql,qr)

3. 从最慢的马开始比较,

  ①当tl<ql时,就用当前田忌的慢马打国王的快马,则tl++,qr--      田忌输

    ②当tl>ql时,就用当前田忌的慢马打国王的慢马,则tl++,ql++     田忌赢

③当tl==ql时:这时最慢的马的速度一样,我们就去考虑快马的情况(开一个循环,直到break)

当田忌最快的马>国王最快的马时(tr>qr),打一局,则tr--,qr--  田忌赢

直到田忌最快的马<国王最快的马时(tr<=qr),用田忌最慢的马打国王当前最快的马,解除相等的局势  ,则tl++,qr--    田忌输

为了防止他们所有的马的速度一样,不论能不能打过都要tl++,qr--,具体看代码


代码:

#include<cstdio>#include<cstring>#include<string>#include<set>#include <map>#include<iostream>#include <cmath>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N (1000020)#define ll long longusing namespace std;int a[2000];//田忌int b[2000];//国王int main(){    int n;    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]);        sort(a,a+n);        sort(b,b+n);        int tl=0,tr=n-1;//田忌的慢马和快马        int ql=0,qr=n-1;//国王的慢马和快马        int sum=0;        while(tl<=tr)        {            if(a[tl]<b[ql])//当前田忌最慢的马小于国王最慢的马            {                sum--;//打一场,战败                tl++;                qr--;            }            else if(a[tl]>b[ql])//当前田忌最慢的马大于国王最慢的马            {                sum++;//获胜                tl++;                ql++;            }            else if(a[tl]==b[ql])//当前田忌最慢的马等于国王最慢的马            {                while(tl <= tr && ql <= qr)//一直到比赛结束                {                    if(a[tr] > b[qr]) //如果田忌最快的马比齐王最快的马要快                    {                        sum++;//获胜                        tr--;                        qr--;                    }                    else                     {                        if(a[tl] < b[qr])//解除局势                            sum--;                        tl++;//为了防止速度都一样                        qr--;                        break;                    }                }            }        }        printf("%d\n",sum*200);    }    return 0;}




原创粉丝点击