贪心法田忌赛马问题Java代码,hdoj 1052 Tian Ji - The Horse Racing【田忌赛马】 【贪心】...

hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

思路:先按从小到大排序, 然后从最快的开始比(假设i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比较,如果等于然后判断,有三种情况:

一:大于则比较,二等于在判断田的最慢的是不是比king的最快的慢,三小于则与king的最快的比较;

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17266    Accepted Submission(s): 5015

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

120849301.gif

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

#include

#include

using std::sort;

int cmp( int a, int b )

{

if( a < b ) return true;

return false;

}

int main()

{

int t[1005], k[1005], n, i, j, c;

while( scanf( "%d", &n ), n )

{

c = 0;

for( i = 0; i < n; i ++ )

scanf( "%d", &t[i] );

sort( t, t+n, cmp );

for( i = 0; i < n; i ++ )

scanf( "%d", &k[i] );

sort( k, k + n, cmp );

i= j = 0;

int flag1= n-1, flag2 = n-1;//flag1是田的

while( i <= flag1 )

{

if( t[flag1] > k[flag2] )

{

++c;

--flag1;

--flag2;

}

else if( t[flag1] == k[flag2] )

{

if( t[i]>k[j] )

{

++c;

++i;

++j;

}

else if( t[i] == k[j] )

{

if( t[i] < k[flag2] ) --c; //注意这处wa了好几次

++i;

--flag2;

}

else if( t[i] < k[j] )

{

--c;

++i;

--flag2;

}

}

else

{

--c;

++i;

--flag2;

}

}

printf( "%d\n", c*200 );

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/561956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML+CSS+JS实现 ❤️仿切水果小游戏❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️卡通足球人物梅西ui特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️svg图片透明层文本显示❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

thinkphp5是不是php,thinkphp5优缺点

ThinkPHP是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。最早诞生于2006年初&#xff0c;2007年元旦正式更名为ThinkPHP&#xff0c;并且遵循Apache2开源协议发布。ThinkPHP从诞生以来一直秉承简洁实用的设计原则&#xff0c;在保持出色的性能和至简的代码的同时&#xf…

毕设/私活/兼职必备,一个挣钱的开源【SpringBoot+Spring Security+MyBatis Plus】脚手架

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、Java李杨勇公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f…

HTML+CSS+JS实现 ❤️canvas手机刮刮乐女朋友效果❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️鼠标悬停性感美女图片特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️美女拼图游戏❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️圆圈倒计时间❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

php通过标识加锁,PHP通过加锁实现并发情况下抢码功能

基于php语言使用加锁实现并发情况下抢码功能&#xff0c;特定时间段开放抢码并不允许开放的码重复&#xff1a;需求&#xff1a;抢码功能要求&#xff1a;1、特定时间段才开放抢码&#xff1b;2、每个时间段放开的码是有限的&#xff1b;3、每个码不允许重复&#xff1b;实现&a…

HTML+CSS+JS实现 ❤️圆形倒数秒计时器❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️从亮到暗图片滤镜特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️Three碎片化图片切换❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️圣诞抓礼物小游戏❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️3D建筑结构旋转特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️H5图片列表滑动特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现 ❤️绘制卡丁车动漫特效❤️

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、【java李杨勇】公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &…

HTML+CSS+JS实现美女canvas仿ps橡皮擦刮刮卡

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、Java李杨勇公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f…

HTML+CSS+JS实现 ❤️贪吃蛇游戏、你能吃过我?❤️【源码送给你一起来玩-建议收藏】

&#x1f345; 作者主页&#xff1a;Java李杨勇 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、Java李杨勇公号作者✌ 简历模板、学习资料、面试题库、技术互助【关注我&#xff0c;都给你】 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f…

oracle02070,ORA-02070:database does not support semijoin错误 | 信春哥,系统稳,闭眼上线不回滚!...

前段时间小姨子遇到了ORA-02070:database does not support semijoin错误:--以下是应用程序报错&#xff1a;at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138) at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)at com.ibm.io.async.Resul…