UVA 1344 / UVALive 3266/ POJ 2287 / HDU 1052Tian Ji -- The Horse Racing

来源:互联网 发布:校园网络贷款 编辑:程序博客网 时间:2024/05/22 00:29

   

   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 inone 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 classhis 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 generalsin Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundredsilver 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 fromthe king, they will certainly lose that round. But then his plus beat the king's regular, andhis super beat the king's plus. What a simple trick. And how do you think of Tian Ji, thehigh ranked official in China?

\epsfbox{p3266.eps}

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting inthe ACM contest right now, he may discover that the horse racing problem can be simply viewedas finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and theking's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw anedge between them, meaning we wish to establish this pair. Then, the problem of winning as manyrounds as possible is just to find the maximum matching in this graph. If there are ties, theproblem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possibleedges, 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 integern (n$ \le$1000) onthe first line, which is the number of horses on each side. The next n integers on the second lineare the speeds of Tian's horses. Then the nextn integers on the third line are the speeds of theking'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 

3 92 83 71  95 87 74 2 20 20 20 20 2 20 19 22 18 0

Sample Output 

200 0 0



题意:田忌赛马,每人各有N匹马,赢/平/输 分别 +200/0/-200银子。让你安排比赛,使得田忌赢的钱最多。


思路:

贪心

1.如果田忌最慢的马速度 > 齐王最慢的马速度,则用他们比。

2.如果田忌最慢的马速度 < 齐王最慢的马速度,则用田忌最慢的马和齐王最快的马比。

3.如果田忌最慢的马速度 = 齐王最慢的马速度:

a.如果田忌最快的马速度 <= 齐王最快的马速度 则让田最慢和齐最快比

b.如果田忌最快的马速度 > 齐王最快的马速度 则最慢的先放一下,让田最快和齐最快比


对于1 2 很好想,3要注意一下。

比如下面这组样例:

3

1 2 3

1 1 2


ans=400


#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxn = 1000 + 20;int A[maxn];int B[maxn];int main() {    int n;    while(scanf("%d", &n) != EOF && 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 ans = 0;        int ai = 0, aj = n-1;        int bi = 0, bj = n-1;        while(ai <= aj) {            if(A[ai] > B[bi]) {                ai++;                bi++;                ans += 200;            } else if(A[ai] < B[bi]) {                ai++;                bj--;                ans -= 200;            } else if(A[aj] <= B[bj]) {                if(A[ai] < B[bj]) ans -= 200;                ai++;                bj--;            } else {                ans += 200;                aj--;                bj--;            }        }        printf("%d\n", ans);    }    return 0;}









0 0
原创粉丝点击