zoukankan      html  css  js  c++  java
  • hdu 1052 Tian Ji -- The Horse Racing(贪心)

    Description

    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."

    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

    很经典的贪心 众所周知的田忌赛马 田忌每赢一场得两百 反之失去两百 

    解题时不仅要考虑最快的马 还要考虑最慢的马

    1.当田忌最慢的马比齐王最慢的马快,赢一场先
    2.当田忌最慢的马比齐王最慢的马慢,和齐王最快的马比,输一场
    3. 如果 最慢的马速度相等
    one.当田忌最快的马比齐王最快的马快时,赢一场先。
    two.当田忌最快的马比齐王最快的马慢时,拿最慢的马和齐王最快的马比,输一场。
    three.当田忌最快的马和齐王最快的马相等时,拿最慢的马来和齐王最快的马比.(田忌最慢的和齐王最快的 有相等和田忌比齐王慢两种情况)

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll __int64
    #define MAXN 1000
    #define INF 0x7ffffff
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    int s1[1000+10];
    int s2[1000+10];
    int vis[1000+10];
    
    int main()
    {
        int n,m,i,j;
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0) break;
            mem(vis,0);
            int ans=0;
            for(i=0;i<n;i++) scanf("%d",&s1[i]);
            for(i=0;i<n;i++) scanf("%d",&s2[i]);
            sort(s1,s1+n);
            sort(s2,s2+n);
            int l1=0,r1=n-1;
            int l2=0,r2=n-1;
            while(l1<=r1)
            {
                if(s1[l1]>s2[l2])
                {
                    ans++;
                    l2++;
                    l1++;
                }
                else if(s1[l1]<s2[l2])
                {
                    ans--;
                    l1++;
                    r2--;
                }
                else
                {
                    if(s1[r1]>s2[r2])
                    {
                        ans++;
                        r1--;
                        r2--;
                    }
                    else if(s1[r1]<=s2[r2])
                    {
                        if(s1[l1]<s2[r2])
                                        ans--;
                        l1++;
                        r2--;
                    }
                }
            }
            printf("%d
    ",ans*200);
        }
        return 0;
    }
    

      

  • 相关阅读:
    WPF应用程序的生命周期
    问题解决(一)在ipad上通过safari浏览文档
    C#网络编程之TCP协议(一)
    认识webMethods
    sql存储过程与webMethods
    Start to study Introduction to Algorithms
    堆和栈的区别 (转贴)
    win7平台下使用MASMPlus搭建汇编环境
    makefile
    struct类型声明的疑问
  • 原文地址:https://www.cnblogs.com/sola1994/p/3909537.html
Copyright © 2011-2022 走看看