zoukankan      html  css  js  c++  java
  • D

    D - D    田忌赛马   解题报告

    hdu 1052 Tian Ji -- The Horse Racing

    链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/D

    题目:

    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

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

    Sample Output

    200 0 0
             
    题意:
    田忌赛马问题,求田忌最多可以赢多少钱,即求田忌的最多净胜场数。多组案例,以0结束。
     
    分析:
    贪心。tq:田忌最快的马   ts:田忌最慢的马
             kq:齐王最快的马   ks:齐王最慢的马
     贪心策略:
    一. tq > kq   -->  tq  VS  kq
    二. tq < kq   -->  ts  VS  kq
    三. tq == kq  --> 1. ts > ks  -->  ts VS ks
                              2. ts < ks  -->  ts VS kq
                              3. ts==ks  -->  ts VS kq
    通过贪心就能求出田忌最多赢得的钱数。
     
    代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=1005;
     6 
     7 int a[maxn],b[maxn];
     8 
     9 int main()
    10 {
    11     int n;
    12     while(scanf("%d",&n)&&n)
    13     {
    14         for(int i=0;i<n;i++)
    15             scanf("%d",&a[i]);
    16         for(int i=0;i<n;i++)
    17             scanf("%d",&b[i]);
    18         sort(a,a+n);
    19         sort(b,b+n);
    20         int tq=n-1,ts=0,kq=n-1,ks=0;
    21         int sum=0;
    22         for(int i=0;i<n;i++)
    23         {
    24             if(a[tq]>b[kq])
    25             {
    26                 sum+=200;
    27                 tq--;
    28                 kq--;
    29             }
    30             else
    31             {
    32                 if(a[tq]<b[kq])
    33                 {
    34                     sum-=200;
    35                     ts++;
    36                     kq--;
    37                 }
    38                 else
    39                 {
    40                     if(a[ts]>b[ks])
    41                     {
    42                         sum+=200;
    43                         ts++;
    44                         ks++;
    45                     }
    46                     else
    47                     {
    48                         if(a[ts]<b[kq])
    49                         sum-=200;
    50                         ts++;
    51                         kq--;
    52                     }
    53                 }
    54             }
    55         }
    56         printf("%d
    ",sum);
    57     }
    58     return 0;
    59 }
     
  • 相关阅读:
    HTML入门(一)
    WEB攻击手段及防御第2篇-SQL注入
    公司来了个新同事不会用 Lombok,还说我代码有问题!
    最流行的 RESTful API 要怎么设计?
    Spring Boot & Restful API 构建实战!
    分布式事务不理解?一次给你讲清楚!
    带着问题学 Kubernetes 架构!
    Linux 与 Unix 到底有啥区别和联系?
    Java虚拟机最多支持多少个线程?
    常用的 Git 命令,给你准备好了!
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4721358.html
Copyright © 2011-2022 走看看