Tian Ji -- The Horse Racing HDU

xiaoxiao2021-02-28  7

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30163    Accepted Submission(s): 9087 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 题目大意: 田忌赛马,田忌和国王赛马,规则马速是衡量马获胜与否的关键,巧妙设计田忌的马和国王的马比赛,求出田忌赛马的最大利润,(输一次-200,赢一次+200),输赢平,三种情况; 首先想要更好的发挥田忌马匹的优势,应当先进性排序,我按照逆序排的,首先,比较田忌第一匹快马和国王第一匹快马,赢则先胜一场,否则(平局或者输了)就先比较田忌最慢的马和国王最慢的马,依然能赢则赢(赢了就拿田忌的第二慢马和国王的第二慢马比较),不赢就去把国王那匹最快的吗ko掉,然后再按照快马比较直到比完田忌所有的马; AC代码: #include<iostream> using namespace std; int main() { int i,j,count,n,a[2][1000],temp,t_first,k_first,t_last,k_last,lose,flag; while (cin>>n&&n) { count=0; lose=0; flag=0; k_first=t_first=0; k_last=t_last=n-1; for(i=0;i<n;i++) cin>>a[0][i]; for(i=0;i<n;i++) cin>>a[1][i]; for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(a[0][j]<a[0][j+1]) { temp=a[0][j]; a[0][j]=a[0][j+1]; a[0][j+1]=temp; } for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(a[1][j]<a[1][j+1]) { temp=a[1][j]; a[1][j]=a[1][j+1]; a[1][j+1]=temp; } while(t_first<=t_last) { if(a[0][t_first]>a[1][k_first]) { count++; t_first++; k_first++; } else if(a[0][t_first]<=a[1][k_first]) { while(a[0][t_last]>a[1][k_last]) { count++; t_last--; k_last--; } if(a[0][t_last]<a[1][k_last]) { t_last--; k_first++; lose++; } else{ if(a[0][t_last]>a[1][k_first]) { count++; t_last--;k_first++; } else if(a[0][t_last]<a[1][k_first]) { lose++; t_last--;k_first++; } else { t_last--;k_first++; } } } } cout<<(count-lose)*200<<endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-1100095.html

最新回复(0)