HDU 1052 Tian Ji-the Horse Racing "the application of greed in dynamic planning"

Source: Internet
Author: User
Tags rounds

Algorithm Analysis:

This problem can obviously be translated into a binary graph best Bets problem. Put the horse on the left and put the King horse on the right. Tian Bogey horse A and King B between, if Tian bogey maxim, then even a right side of 200; If a draw, a 0 edge is connected, and if it loses, it has a right edge of 200. However, we know that the best matching algorithm of the binary graph is of high complexity and cannot meet the requirements of n=2000. We may as well use the greedy thought to analyze the question. Because Tian Bogey master has the "initiative" of the game, he always according to king out of the horse to distribute his horse, so it may be considered that the King order is the horse speed from high to low. From this hypothesis, we sum up the following greedy strategies:

? 1, if the most powerful horse in the rest of the horse can not win Qi Wang remaining the strongest horse, then should use the worst horse to lose to King the strongest horse. 2, if the most powerful horse in the rest of the horse can win the king of the rest of the strongest horse, then use this horse to win the remaining king of the most powerful horse. 3, if the most powerful horse in the remaining horse and king the rest of the strongest horse flat, you can choose to play flat or with the worst horse to lose the game.

A proof of the first greedy strategy: at this time all the horses do not win King horse, so no matter with the slowest horse to lose or use the fastest horse to lose all the same is lost, and in the greedy thought, we should keep the comparison stronger horse, so with the slowest horse to lose will not be worse than with other horses to lose, so this is the optimal strategy. The certificate is completed.

The second greedy strategy proof: Assume now the most powerful horse is a, Tian bogey remaining the strongest horse is B, if there is a better game strategy, let B's opponent is not A, and make the field bogey win more money, then set at this time a opponent is b,b opponent is a:1, if b>a, then there is b >a,b>a. The result is the same as the b>a,b>a. 2, if a<b≤a, there is b>a,b≤a. This result is not as good as b>a,b>a. 3, if b≤a≤a, there is b>a,b≤a. The result is the same as the b>a,b≤a. Thus, the exchange of their opponents, will not make the results worse, then the hypothesis is not tenable. The certificate is completed.

The third greedy strategy proof: Because Tian bogey the fastest horse also just and king of the horse, then field bogey can only choose flat or lose, choose flat, of course, can only use the fastest horse to flat, and choose to lose the words at that time is the slowest horse to lose is worth, this and the first greedy strategy thinking is the same. The certificate is completed.

We found that the third greedy strategy came up with a branch: Flatten or lose. If all the cases are exhaustive, the complexity of the algorithm will be higher than the best matching of the binary graph; If generalize's choice allows the strongest horse to play the game or let the worst horse lose the game, there is a counter-example:? Just flat, if the speed of the Zi Wangma is 1 2 3, the speed of the field is also 1 2 3, each choice to play flat, Tian bogey a penny can not get, and if you choose to use the speed of 1 horse to the speed of 3 of the horse, you can win 2002 gold. Just lose the words, if the speed of the Zi Wangma is 1 3, the field of the speed of the horse is 2 3, Tian bogey a win a negative, still a penny can not get. And if a horse with a speed of 3 goes flat, it can win 2002 gold.

Although because of the third greedy branch, we can not directly in accordance with this method to design a completely greedy method, but through the above three kinds of greedy strategy, we can find that if the King horse is sorted by speed, from high to low is sent, Tian bogey must be his horse by the speed of sorting, Take a horse from both ends and race against the King Horse. With this information, the dynamic programming model is out! F[I,J] said that Qi Wang according to the order from strong to weak, Hetian bogey after I game, from the "head" took a strong J horse, from the "tail" took the i-j horse, the biggest profit can be obtained.   The state transfer equation is as follows: F[i][j]=max (f[i-1][j]+g[n-(i-j) +1][i],f[i-1][j-1]+g[j][i]); Which G[i,j] said that the horse and King of Tian Bogey in order from strong to weak, the first horse of Tian Bogey and King of the first J horse race can make profit, win 200, lose 200, flat 0. Summary: Although there is a direct greedy method, but it can be used as an example to tell everyone, rational use of greedy strategy, analysis of some of the nature of the problem, some seemingly can not use dynamic planning to do the topic can be cleverly established state, and then can be solved by dynamic programming.

Added: for state transition equations

F[i][j]=max (f[i-1][j]+g[n-(i-j) +1][i],f[i-1][j-1]+g[j][i]);

There should be two special cases.

1 in the first game, 0 horses were taken from the beginning, then all the horses were taken from the tail, then f[i][j]=f[i-1][j]+g[n-(i-j) +1][i];

where n (i-j) +1 represents the first horse in the whole sequence of horses from the tail (i-j) horse

For example, there were 8 horses, and in the 4th game, 0 horses were taken from the top, indicating that 4 horses were to be taken from the tail.

1 2 3 4 5 6 7 8

That is to take the horse numbered 5, 5=8-(4-0) +1; f[4][0]=f[3][0]+g[8-(4-0) +1][4]

2 in the first game, I picked up the horse from the beginning, indicating that the horse f[i][j]=f[i-1][j-1]+g[j][i was always taken from the beginning.

Tian Ji--The Horse Racing

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 19321 Accepted Submission (s): 5641

Problem Descriptionhere is a famous stories in Chinese.
"That's about 2300 years ago." General Tian Ji is a high official in the country Qi. He likes to play horse racing with the king and others. "
"Both of Tian and the king had three horses in different classes, namely, regular, plus, and super. The rule is to has three rounds in a match; Each of the horses must is used in one round. The winner of a single round takes and hundred silver dollars from the loser. "
"The Being the most powerful man in the country, the king had so nice horses, and each class he horse is better than Tian ' S. As a result, each time the king takes six hundred silver dollars from Tian. "
"Tian Ji is not happy on 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 of hundred silver dollars and such a grace in the next match. "
"It is a rather simple trick. Using the He regular class horse race against the Super class from the king, they would certainly lose that round. But then he plus beat the King ' s regular, and his super beat the King's Plus. What's a simple trick. And how does your think of Tian Ji, the high ranked official in China? "

Were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he could discover that the horse racing problem can be simply Viewe D 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 a edge between them, meaning we wish to establish this Pair. Then, the problem of winning as many rounds as possible are just to find the maximum matching in this graph. If There is 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 are a too advanced tool to deal with the problem.
In this problem, you is asked to the write a program to solve this special case of matching problem. Inputthe input consists of up to test cases. Each case starts with a positive an integer n (n <=) on the first line, which are the number of horses on each side. The next n integers on the second line is the speeds of Tian ' s horses. Then the next n integers in the third line is the speeds of the King ' s horses. The input ends with a line is has a single 0 after the last test case. Outputfor each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in Silve R dollars. Sample Input392 7195 74220 2020 20220 1922, sample Output20000

#include <stdio.h> #include <string.h>int tian[1005],king[1005],g[1005][1005],f[1005][1005];void Bubblesort (int a[],int n) {int i,j,t;for (i=1;i<=n;i++) {for (j=i+1;j<=n;j++) {if (A[i]<a[j]) {T=a[i];a[i]=a[j] ; a[j]=t;}}} int max (int a,int b) {if (a>b) return A;elsereturn B;} int main () {int n,i,j,maxx;while (scanf ("%d", &n)!=eof&&n)//input is 0 when the end of input {for (i=1;i<=n;i++) scanf ("%d", &tian[i]); for (i=1;i<=n;i++) scanf ("%d", &king[i]); memset (G,0,sizeof (g)); Memset (F,0,sizeof (f)); Bubblesort (king,n); Bubblesort (tian,n); for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (Tian[i]>king[j]) g[i][j]= 200;else if (tian[i]<king[j]) g[i][j]=-200;elseg[i][j]=0;}} for (i=1;i<=n;i++) {for (j=0;j<=i;j++) {if (j==0) f[i][j]=f[i-1][j]+g[n-(i-j) +1][i];else if (i==j) f[i][j]=f[i-1]        [J-1]+g[j][i];        Else F[i][j]=max (f[i-1][j]+g[n-(i-j) +1][i],f[i-1][j-1]+g[j][i]); }}maxx=-200005;for (j=0;j<=n;j++) {if (F[n][j]>maxx) maxx=f[n][j];} printf ("%d\n", Maxx);}}

  

HDU 1052 Tian Ji-the Horse Racing "the use of greed in dynamic planning"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.