1052 Tian Ji -- The Horse Racing

2023-10-28 06:50
文章标签 1052 ji tian horse racing

本文主要是介绍1052 Tian Ji -- The Horse Racing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目详情: 

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47942    Accepted Submission(s): 14651

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
 
Source
2004 Asia Regional Shanghai
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1051 1045 1800 1053 1789 
  

题目大意:

田忌和国王赛马看谁的马快,匹数均为n,田忌每赢国王一局,+200元,输一局-200元,平局+0元,

问如何安排比赛马的出场比赛顺序可以使田忌尽可能多赢钱。

 解题思路:

先把田忌的马和国王的马分别降序排序,如果田忌的快马比国王的快马快,则胜局+1,否则,

拿田忌的慢马和国王的慢马比较,慢马能赢就赢,如果田忌的慢马输了再拿田忌的慢马和国王的快马进行比较,这里贪心比较明显,用自己最慢的马去浪费对方最快的马,反正走到这步肯定没有马可以跑赢对面的快马,干脆拿慢马去消耗掉一个比赛的机会。

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){return a>b;
}
int main(){int n;//每次双方各有多少匹马int horse;//马(用于输入)while(cin>>n&&n!=0){vector<int> tianji;//田忌的马vector<int> king;//皇帝的马int sum_win=0;//田忌赢的场数int tianji_fast=0;//田忌的快马int tianji_slow=n-1;//田忌的慢马int king_fast=0;//国王的快马int king_slow=n-1;//国王的慢马for(int i=0;i<n;++i){cin>>horse;tianji.push_back(horse);}for(int i=0;i<n;++i){cin>>horse;king.push_back(horse);}sort(tianji.begin(),tianji.end(),cmp);//降序排序sort(king.begin(),king.end(),cmp);//降序排徐for(int i=0;i<n;++i){if(tianji[tianji_fast]>king[king_fast]){//田忌的快马赢++tianji_fast;//田忌下一匹快马++king_fast;//国王下一匹快马++sum_win;//赢的次数+1}//执行到这说明田忌的快马没有国王的快马快else if(tianji[tianji_slow]>king[king_slow]){//田忌的慢马比国王的慢马快--tianji_slow;//田忌的上一匹慢马--king_slow;//国王的上一匹慢马++sum_win;//赢的次数+1}//执行到这说明田忌的慢马没有国王的慢马快else if(tianji[tianji_slow]<king[king_fast]){//田忌的慢马比国王的快马慢--tianji_slow;//田忌的上一匹慢马++king_fast;//国王的下一匹快马--sum_win;//输了,赢的次数-1}}cout<<sum_win*200<<endl;//输出}return 0;
}

这篇关于1052 Tian Ji -- The Horse Racing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/291453

相关文章

nyoj 1037 Postscript of Tian Ji racing

一道卡贪心的题。 也算一道改编题。 此题的解法推荐为二分图的最大匹配。 首先将输入数据转换一下,然后将满足题意的一组牌建立条边,最终边的覆盖数即为 LN 最后可得的分数。 然后求出最大匹配即可。 代码如下: #include<stdio.h>#include<string.h>char card[30][5];char s[5];int map[30][30];

HDU 1052(贪心)

题意:田忌赛马。田忌和齐王各有n匹马,输入田忌的马的速度和齐王的马的速度。每一轮田忌赢了就得200两银子,平就得0两,输了就失去200两银子。问田忌最多能得到多少。题目的策略是贪心,分析见leokan大牛的blog,这里也转一下,留存根。 http://hi.baidu.com/leokan/blog/item/126da06e1dab5ade80cb4a4f.html 算法可以用DP,

【九度】题目1052:找x

题目1052:找x 时间限制:1 秒内存限制:32 兆特殊判题:否提交:4671解决:2504 题目描述: 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。 输入: 测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。 输出: 对于每组输入,请输出结果。 样例输

POJ2287 HDU1052 Tian Ji -- The Horse Racing【贪心】

题目链接: http://poj.org/problem?id=2287 http://acm.hdu.edu.cn/showproblem.php?pid=1052 题目大意: 田忌和大王赛马,两个人各有N匹马,每匹马都有一个速度,跑的快的胜,慢的就输。田忌每赢一 把得200,平了不得钱,输了输200。每次大王先出马,田忌再出马。问:田忌最多能得多少钱。 思路: 贪

wikioi 1052 大顶堆

题目描述 Description     王钢是一名学习成绩优异的学生,在平时的学习中,他总能利用一切时间认真高效地学习,他不但学习刻苦,而且善于经常总结、完善自己的学习方法,所以他总能在每次考试中得到优异的分数,这一切很大程度上是由于他是一个追求效率的人。     但王钢也是一个喜欢玩的人,平时在学校学习他努力克制自己玩,可在星期天他却会抽一定的时间让自己玩一下,他的爸爸妈妈

UVA 11766 - Racing Car Computer(DP)

题目链接:11766 - Racing Car Computer 题意:n个人进行比赛,以下n行输入对于每个人而言,有a个人在他前面,b个人在他后面。可能并排,问根据所有人情况,找出矛盾最小的数目。 思路:这题只要想通一点就很简单了。 对于每个人而言,他的位置可能的区间为[a + 1, n - b]。 那么对于两个人而言,如果他们可能区间相交,那么肯定矛盾,反之则不矛盾。 证明

Tian Ji -- The Horse Racing(考虑周到或者省去相等)

Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 8 Font: Times New Roman | Verdana |

03_led_horse_run_v0 跑马灯

03_led_horse_run_v0 在Verilog中实现跑马灯通常涉及到使用一个计数器来控制LED灯的亮灭顺序。 跑马灯是一种常见的电子显示方式,它通过控制多个LED灯的顺序点亮,形成一种动态的视觉效果,看起来就像灯在“跑”一样。 知识点: 移位寄存器 module led_horse_run #(parameter CLK_FREQ = 50*1000*1000)(input

1052. 【NOIP2016备赛】方阵操作(square)

1052. 【NOIP2016备赛】方阵操作(square)  (Input: square.in, Output: square.out) 时间限制: 1 s 空间限制: 256 MB  题目描述 小 Z 给你一个 n × n 的方阵,要求你完成 Q 次操作: 1. 1 i j k,将 ai,j 修改为 k。 2. 2 i j,交换方阵的第 i 行和第 j 行。 3. 3 i j,交换方

LeetCode 1052. 爱生气的书店老板

题目链接 https://leetcode.cn/problems/grumpy-bookstore-owner/description/?envType=daily-question&envId=2024-04-23 先把最初的满意人数累加算出来,然后使用滑动窗口来模拟连续 minutes分钟不生气,计算不生气minutes分钟最大的满意数 class Solution {public