博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1052
阅读量:6760 次
发布时间:2019-06-26

本文共 4265 字,大约阅读时间需要 14 分钟。

                          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
392 83 7195 87 74220 2020 20220 1922 180
 
Sample Output
20000
 

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

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

我写的c++代码:

1 #include
2 using namespace std; 3 4 void sort(int a[],int t) //从高到低排序 5 { 6 for(int i=0;i
>n&&(n!=0))17 {18 for(i=0;i
>ti[i];20 sort(ti,n);21 for(i=0;i
>king[i]; 23 sort(king,n); 24 tfast=kfast=0;25 tslow=kfast=n-1;26 sum=0;27 for(i=0;i
king[kslow]){sum++;tslow--;kslow--;}32 else if(ti[tslow]
king[kfast]){sum++;tfast++;kfast++;}41 42 }43 cout<
<

能正确提交的c代码:

1 #include
2 void sort(int a[],int t) //从高到低排序 3 { 4 int temp,i,j; 5 for(i=0;i
w[wslow]){sub++;tslow--;wslow--;}36 else if(t[tslow]
w[wfast]) //情况244 {sub++;tfast++;wfast++;}45 else {sub--;tslow--;wfast++;} //情况346 }47 sum=sub*200;48 printf("%d\n",sum);49 sub=0;sum=0;50 }51 return 0;52 }

 

转载于:https://www.cnblogs.com/mm-happy/p/3800950.html

你可能感兴趣的文章
****RESTful API 设计最佳实践(APP后端API设计参考典范)
查看>>
Linux内存管理-高端内存(二)
查看>>
[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
查看>>
FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法。。
查看>>
[LeetCode] 4Sum 四数之和
查看>>
Hello World 之 控制台版本(Console Application)
查看>>
IOS 播放动态Gif图片
查看>>
随笔1
查看>>
HTML中Select的使用具体解释
查看>>
Java synchronized
查看>>
mysql大内存高性能优化方案
查看>>
VSTO 学习笔记(十二)自定义公式与Ribbon
查看>>
[再寄小读者之数学篇](2015-06-24 Series)
查看>>
【Linux】linux常用基本命令
查看>>
4-python学习——数据操作
查看>>
Oracle函数
查看>>
Unity3D学习笔记第一课
查看>>
【redis使用全解析】常见运维操作
查看>>
hdu2377Bus Pass(构建更复杂的图+spfa)
查看>>
Vc6.0打开该文件坠毁
查看>>