zoukankan      html  css  js  c++  java
  • HDU-1052(贪心策略)

    Tian Ji -- The Horse Racing

    Problem 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

    分析:题意就是田忌赛马的扩展,田忌和国王每个人有n匹马,然后两个人每匹马两两对应匹配,谁的马速度快谁就赢一局,求怎么匹配使得田忌赢的局数尽可能多。

            题目有个误解让人以为是二分图最大匹配,其实是个贪心,但对我来说不好想。分三种情况:

            1.田忌最慢的马比国王最慢的马还慢,则拿去与国王最快的马PK,反正都是输,不如拿去把对方最快的比掉,则后面马赢的机会就更大。

       2.田忌最慢的马比国王最慢的马快,则田忌赢一局。

        3.田忌最慢的马与国王最慢的马速度相等,则不能打平,因为打平与一平一负效果等价,还不如把田忌最慢的马去与国王最快的马比,为后面的马争取更大的赢的机会。

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <sstream>
    10 #include <queue>
    11 #include <typeinfo>
    12 #include <fstream>
    13 #include <map>
    14 #include <stack>
    15 using namespace std;
    16 #define INF 100000
    17 typedef long long ll;
    18 const int maxn=1010;
    19 int tian[maxn],king[maxn];
    20 int main()
    21 {
    22 //    freopen("in.txt","r",stdin);
    23 //    freopen("out.txt","w",stdout);
    24     int n;
    25     while(scanf("%d",&n)){
    26         if(n==0) break;
    27         memset(tian,0,sizeof(tian));
    28         memset(king,0,sizeof(king));
    29         for(int i=0;i<n;i++)
    30             scanf("%d",&tian[i]);
    31         for(int i=0;i<n;i++)
    32             scanf("%d",&king[i]);
    33         sort(tian,tian+n);
    34         sort(king,king+n);
    35         int win=0,lose=0;
    36         int t_low=0,t_best=n-1,k_low=0,k_best=n-1;
    37         while(t_low<=t_best){
    38             if(tian[t_low]>king[k_low]){
    39                 win++;
    40                 t_low++;
    41                 k_low++;
    42             }
    43             else if(tian[t_low]<king[k_low]){
    44                 lose++;
    45                 t_low++;
    46                 k_best--;
    47             }
    48             else{
    49                 if(tian[t_best]>king[k_best]){
    50                     win++;
    51                     t_best--;
    52                     k_best--;
    53                 }
    54                 else{
    55                     if(tian[t_low]<king[k_best])
    56                         lose++;
    57                     t_low++;
    58                     k_best--;
    59                 }
    60             }
    61         }
    62         int ans=(win-lose)*200;
    63         printf("%d
    ",ans);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners
    C++ 第八天
    C++ 第七天
    C++ 第四天
    c++ 编译报错汇总(随时更新)
    C++ 第二天
    C++ 第三天
    c++ 继承(二)
    c++ 继承(一)
    回调函数
  • 原文地址:https://www.cnblogs.com/RRirring/p/4721827.html
Copyright © 2011-2022 走看看