HDOJ1052:Tian Ji — The Horse Racing–简单贪心

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 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 3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0 Sample Output 200 0 0 贪心问题的简单应用.首先将田忌的马和齐王的马做个排序.然后分类讨论

  • 田忌最快的马比齐王最快的快–直接跑赢;
  • 田忌最快的马比齐王最快的慢–用田忌最慢的马去跟齐王的马跑;
  • 田忌最快的马跟齐王最快的一样快–再比较田忌最慢的马是否比齐王的最慢的马快,如果是就直接跑赢,如果不是就用田忌最慢的马去跟齐王最快的马跑.
  • 放上代码:

    HDOJ1009:FatMouse' Trade——简单贪心

    Problem Description
    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
    The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

    The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

    For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

    Sample Input
    5 3
    7 2
    4 3
    5 2
    20 3
    25 18
    24 15
    15 10
    -1 -1

    Sample Output


    POJ3617:Best Cow Line——字典序最小问题


    FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual”Farmer of the Year” competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

    The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows’ names.

    FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

    FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he’s finished, FJ takes his cows for registration in this new order.

    Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.


    * Line 1: A single integer: N
    * Lines 2..N+1: Line i+1 contains a single initial (‘A’..’Z’) of the cow in the ith position in the original line


    The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows (‘A’..’Z’) in the new line.

    Sample Input

    Sample Output





    1. 按照字典序比较S和反转后的字符串S’
    2. 如果S较小,就从S开头取出字符,放到T的末尾
    3. 如果S’较小,就从S末位取出字符,放到T的末尾
    4. 如果相同,则比较(S+1)和(S’-1)两字符的大小;相同则继续。














    • 在可选工作中,每次选取最先开始的工作
    • 在可选工作中,每次选取用时最短的工作
    • 在可选工作中,每次选取与最少可选工作有重叠的工作
    • 在可选工作中,每次选取结束时间最早的工作







    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 27554 Accepted Submission(s): 14558
    Problem Description


    输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。


    Sample Input
    1 3
    3 4
    0 7
    3 8
    15 19
    15 20
    10 15
    8 18
    6 12
    5 10
    4 14
    2 9

    Sample Output







    • 感谢邀请。
    • 网络上流传的答案有很多,估计提问者也曾经去网上搜过。所以根据自己微薄的经验提点看法。
    • 我ACM初期是训练编码能力,以水题为主(就是没有任何算法,自己靠动脑筋能够实现的),这种题目特点是麻烦,但是不难,30-50道题目就可以了。
    • 然后可以接触一下基础的算法,我感觉搜索方向的比较不错,可以解决很多问题,深搜,广搜,然后各种剪枝能力的锻炼。
    • 搜索感觉不错了就可以去看看贪心,图论,和动态规划方向的了。图论有最短路径,最小生成树,网络流,拓扑排序等等很多,动态规划先去书上看经典例子,最长公共子序列等。各种变形的题目。
    • 数学是ACM中极具杀伤力的武器,我一向很羡慕数学好的队友,精力有限自己数学方面的算法只能说入门。这方面经典的数论,组合数学方面的比较多,计算几何是很重要的,经典模型要熟悉,最近点对,二维三维,凸包以及各种应用。
    • 数据结构方面的就比较多了,基础的堆,栈,队列,并查集,二叉查找树,红黑树,trie树,hash表等等。
    • 用C++参赛的话STL要熟悉,有时候很有帮助,里面的queue,list,map,stack等。
    • ACM到后来算法就成了工具,不断的靠自己意淫一个新的解法来解决问题是最开心的事情了。我们学校ACM一直是一届带一届的,老师只提供经济上的援助,上面的内容是我在大三当队长时教给大一的新队员的入门内容,再深的就靠每个人自己发掘了。