HDU 1052 Tian Ji -- The Horse Racing (Greedy)

Source: Internet
Author: User
Tags rounds

Tian Ji -- The Horse RacingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 14907 Accepted Submission (s ): 4286 Problem DescriptionHere 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 take S 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 th At 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 M, meaning we wish to establish this pair. then, the problem of winning as your 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. inputThe 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. outputFor each input case, output a line containing a single number, which is the maximum Money Tian Ji will get, in silver dollars. sample Input392 83 7195 87 74220 2020 20220 1922 Sample Output20000 question meaning: I understand the meaning of the question very well. I will give you n horses of Tian Ji first, give you the n horses of King Qi. Ask how many points you can score. If you win a game with 200 points, you lose one game with 200 points. Solution: the greedy strategy is to first use Tian Ji's fast horse to compare with Qi Wang's fast horse,> win one game, <then Tian Ji's slow horse is compared with Qi Wang's fast horse. = It is not well handled here, So WA is the case. I read Daniel's blog. Let's talk about the ideas. The idea is to make a comparison between a slow horse and a slow horse. If you win a game, <pull the horse into the water. = it depends on whether your fast horse can win or not. If you cannot win the game, you can win the game. The text below comes from nyist_xiaod1. If Tian Ji's slowest horse can beat Qi Wang's slowest horse, then let it beat the slow horse and win the game and Add 1. (Tian Ji slowest horse> Qi Wang slowest horse) 2. if Tian Ji's slowest horse cannot beat Qi Wang's slowest horse, then he cannot beat other horses even more, so he will lose and lose to Qi Wang's fastest horse, and the number of failures increases by 1. (Tian Ji's slowest horse <Qi Wang's fastest horse) 3. If Tian Ji's slowest horse is the same as QI Wang's slowest horse. At this time, we cannot simply think that it is the best case to strike it together. On the contrary, flattening is the next strategy. Why? Because the teammates behind them are likely to win the slow horse at this time, even if they lose one game, they can also help win one game, the benefits of losing one game are the same as those of balancing one game, and when you lose, you can bring the fastest horse into the game, create a greater chance of victory for your fastest horse (because it has lost a strong opponent). That is to say, the fastest horse on your side is likely to win another game at your sacrifice, from this perspective, it is better to lose it on your own. However, you must note that before you release the water, if your fastest horse is faster than the fastest horse on the other side, and then you lose to the fastest horse on the other side, so the talents of our fastest horse are wasted. Why? It is easy. It could have been won. Do you need to release water? --! In other words, in this case, your sacrifice has no value. Therefore, when releasing water, you must ensure that your fastest horse is not as fast as the other party's fastest horse. When this condition is met, allow the slowest horse to compete with the fastest horse of the other party (a draw may be possible). In this way, Tian Ji's horse is fully utilized. AC code:

# Include <iostream> # include <cstring> # include <algorithm> # include <cstdio> using namespace std; int a [1005]; int B [1005]; int cmp (int a, int B) {if (a> B) return 1; return 0;} int main () {int n, I, j, sum, len1, len2; while (scanf ("% d", & n) {for (I = 0; I <n; I ++) scanf ("% d ", & a [I]); sort (a, a + n, cmp); // sort horses from fast to slow (I = 0; I <n; I ++) scanf ("% d", & B [I]); sort (B, B + n, cmp ); // Qi Wang's horses are sorted from fast to slow. I = 0, j = 0; len1 = len2 = n-1; // The Position of the slowest horse is sum = 0; while (I <= len1 & j <= len2) {if (a [len1]> B [len2]) // Tian Ji Qian Ma> Qi Wang Qian Ma {sum ++; // win the first game len1 --, len2 --;} else if (a [len1] <B [len2]) {sum --; // take the slowest horse and Qi Wang's fast Maasai j ++; len1 --;} else {if (I <len1) // remove the fastest horse and other horses {if (a [I] <= B [j]) // Tian Ji's fast horse cannot win {if (a [len1] <B [j]) sum --; len1 --, j ++;} else {sum ++; // fast horse wins I ++; j ++ ;}} else {len1 --, len2 --; // draw }}printf ("% d \ n ", sum * 200);} return 0;}/* 470 65 50 2070 65 55 20470 60 50 2070 65 20470 55 50 2070 65 55 20 * // 15 MS 236 K

 


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.