hdu 1052 Tian Ji Tian Ji Horse Racing【Greedy】(Water Question)

Question link: https://vjudge.net/problem/HDU-1052

Reprinted in >>>

 

Topic meaning:

The story of Tian Ji's horse racing is well known. Tian Ji and the king race horses, earning 200 for a win, and a loss of 200 for a loss. The draw has no effect. Ask Tian Ji how much he can earn.
Input:
Input data up to 50 test cases. Each data starts with a positive integer n (1000), which is the number of horses. Each integer in the second line is the speed of Tian Ji's horse. Each integer in the third row is the speed of the king's horse. When n is 0, the input ends.
Output:
For each input case, the output line contains a single number, which is the maximum amount of money that Tian Ji will receive.

 

To be honest, I still don't understand this question very well. Why is Tian Ji's fastest horse faster than Qi Wang's fastest horse?

For example: 100 80 75 70 65

              98      81    76   72    67

This set of data obviously does not match. Forget it, record this question first and look at it later.

Besides, why is this question a greedy question?

#include<iostream>
using namespace std;
bool cmop(int a,int b)
{
     return a>b;
}
intmain ()
{
    int i,j,h,n,king[1005],tianji[1005],kquick,kslow,tquick,tslow,total;
    while(cin>>n,n)
    {
    total=0;
      for(i=0;i<n;i++)
        cin>>tianji[i];
      for(i=0;i<n;i++)
        cin>>king[i];
      sort(king,king+n,cmop);
      sort(tianji,tianji+n,cmop);
      //kquick=king's the most quick ,kslow =king's the most slow 
      kquick=tquick=0;
      kslow =tslow=n- 1 ;
       for (i= 0 ;i<n;i++) // i is tianji the most quick 
      {
          // When Tian Ji's fastest horse is faster than the king's fastest horse, use Tian Ji's fastest horse The horse is better than the king's fastest horse 
         if (tianji[tquick]> king[kquick])
         {
             total+=200;
             kquick++;
             tquick++; 
         }
         // When Tianji's fastest horse is slower than the king's fastest horse, use Tianji's slowest horse to compare with the king's fastest horse 
         else  if (tianji[tquick]< king[kquick])
         {
              total-=200;
              tslow--;
              kquick++;
         }
         else//j is tianji  
         {
             for(j=tslow,h=kslow;j>=tquick;j--,h--)
             {
                 if (tianji[j]>king[h]) // When Tianji's slowest horse is faster than the king's slowest horse, use Tianji's slowest horse to compare with the king's slowest horse 
                 {
                     total+=200;
                     tslow--;
                     kslow--;
                 }
                 else  // Compare Tian Ji's slowest horse with the king's fastest horse 
                 {
                      if (tianji[j]< king[i])
                     {
                         total-=200;
                         tslow--;
                         kquick++;
                     }
                     tslow=--j;
                     kslow=h;
                     break;
                 }
             }
         }
         if(tquick>tslow)
           break;
      }
      
      cout<<total<<endl;
    }
    return 0;

 

2018-04-24

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324794853&siteId=291194637