• hdu1052


                              Tian Ji -- The Horse Racing

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3900 Accepted Submission(s): 935
     
    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
     

    涉及知识点:贪心算法,排序,动态规划;

    我用c++写的代码会出错,改用c语言写后却正确ac,不明白为什么求大神指点一二!!

    我写的c++代码:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void sort(int a[],int t)      //从高到低排序
     5 {
     6     for(int i=0;i<t;i++)
     7         for(int j=i+1;j<t;j++)
     8             if(a[i]<a[j])
     9                 swap(a[i],a[j]);
    10 }
    11 
    12 int main()
    13 {
    14     int ti[50],king[50];
    15     int n,i,tfast,kfast,tslow,kslow,sum;
    16     while(cin>>n&&(n!=0))
    17     {
    18         for(i=0;i<n;i++)
    19             cin>>ti[i];
    20             sort(ti,n);
    21         for(i=0;i<n;i++)
    22             cin>>king[i];    
    23             sort(king,n);            
    24         tfast=kfast=0;
    25         tslow=kfast=n-1;
    26         sum=0;
    27         for(i=0;i<n;i++)
    28         {
    29             if(ti[tfast]==king[kfast])
    30             {
    31                 if(ti[tslow]>king[kslow]){sum++;tslow--;kslow--;}
    32                 else if(ti[tslow]<king[kslow]){sum--;tslow--;kfast++;}
    33                 else if(ti[tslow]==king[kslow])
    34                 {
    35                     if(ti[tslow]<king[kfast]){sum--;tslow--;kfast++;}
    36                     else if(ti[tslow]==king[kfast]){    break;}                  //error?
    37                 }
    38             }
    39             else if(ti[tfast]<king[kfast]){sum--;tslow--;kfast++;}
    40             else if(ti[tfast]>king[kfast]){sum++;tfast++;kfast++;}
    41                 
    42         }
    43         cout<<sum*200<<endl;
    44         
    45     }
    46     
    47     return 0;
    48 }

    能正确提交的c代码:

     1 #include<stdio.h>
     2 void sort(int a[],int t)      //从高到低排序
     3 {
     4     int temp,i,j;
     5     for(i=0;i<t;i++)
     6         for(j=i+1;j<t;j++)
     7             if(a[i]<a[j])
     8             {
     9                 temp=a[i];
    10                 a[i]=a[j];
    11                 a[j]=temp;
    12             }    
    13 }
    14 int main()
    15 { 
    16     int n,i,sub=0,sum=0;
    17     int tfast,tslow,wfast,wslow;  //分别代表田的最快马,最慢马,王的最快马,最慢马
    18     int t[1000],w[1000];
    19     while(scanf("%d",&n)&&(n!=0))
    20     {   
    21         for(i=0;i<n;i++)
    22             scanf("%d",&t[i]);
    23         for(i=0;i<n;i++)
    24             scanf("%d",&w[i]);
    25         sort(t,n);
    26         sort(w,n);
    27 
    28         tfast=0;tslow=n-1;
    29         wfast=0;wslow=n-1;    //最快最慢马的初始化
    30    
    31         for(i=0;i<n;i++)         //分三种情况
    32         {
    33             if(t[tfast]==w[wfast])    //情况1
    34             {
    35                 if(t[tslow]>w[wslow]){sub++;tslow--;wslow--;}
    36                 else if(t[tslow]<w[wslow]){sub--;tslow--;wfast++;}
    37                 else if(t[tslow]==w[wslow])
    38                 {
    39                     if(t[tslow]<w[wfast]){sub--;tslow--;wfast++;}
    40                     else if(t[tslow]==w[wfast]){break;}
    41                 }
    42             }
    43             else if(t[tfast]>w[wfast])     //情况2
    44             {sub++;tfast++;wfast++;}
    45             else    {sub--;tslow--;wfast++;} //情况3
    46         }
    47         sum=sub*200;
    48         printf("%d
    ",sum);
    49         sub=0;sum=0;
    50     }
    51   return 0;
    52 }
  • 相关阅读:
    Cipherlab CPT9300手持扫描枪开发体验 [转]
    引用(ajaxfileupload.js) ajaxfileupload.js报jQuery.handleError is not a function错误解决方法
    C#锐利体验2.0:泛型编程
    Visual C#中调用Windows服务初探
    C#操作XML代码整理
    个人代码库のC#背景色渐变的功能
    ~~ C#数字时钟 ~~
    DevExpress随笔10.1.5的汉化与破解
    用C#获取局域网内所有机器
    C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标
  • 原文地址:https://www.cnblogs.com/mm-happy/p/3800950.html
Copyright © 2020-2023  润新知