hdu 1052 Tian Ji -- The Horse Racing

SAP S4 MM模块自动记账全流程解析:从评估范围到科目分组的实战配置 本文深入解析SAP S4 MM模块自动记账的全流程,从核心概念评估范围与评估控制入手,详细阐述了评估类、科目分组等关键配置的逻辑关系。通过实战配置步骤,指导如何建立从物料移动(如收货、发料)到自动生成准确财务凭证的完整体系,确保库存价值与总账实时同步,为企业构建可靠的财务业务一体化基石。 阅读详情

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1052

 

题目描述:

Tian Ji -- The Horse Racing

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


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

 

题意:田忌赛马......问给出两列马的战斗力,问田忌最多能赚多少。

 

题解:明显贪心了,一开始自己贪心策略想简单了,想得是尽快将能将就吧大王的马赢了的就尽管赢,赢不了的马就拿去抵掉大王的那些很牛逼的马。实际上基本策略是这样贪的。

 

 

 

但根据几组实验的测试数据要注意:

一、

2
1 2
1 3

若最慢的马相等,看最快的马如果最快的马田忌比不了大王的,就用最慢的田忌马去抵掉大王的最快马

 

二、

 9
80 140 76 58 93 140 146 74 173
97 80 135 72 0 107 155 66 141

若最慢的马相等,若最快的马田忌的比大王的快,那么就让最快马比,接着放空这组最慢相等马,继续从最快比。注意这里的最慢最快都是递归的,因为,一旦一对马比完就被淘汰了,不能参加两次比赛,这样最慢 最快的马会变成另外的。所以这里的最慢最快都是递归的。

 

 

所以综合这两种测试数据 得出完整的贪心策略:(把马从大到小排序)

先从最慢的马依次比:1、田忌最慢马比大王最慢马快,直接比赛

                                        2、田忌最慢马比大王最慢马慢,让田忌最慢马与大王最快马比赛(反正它都是输,就让它输得更有价值一点,把对面的一个强劲的对手pass掉)

                                        3、田忌最慢马与大王最慢马相等:(这时放空最慢马,从最快马来讨论)

                                                            (1)、田忌最快马比大王最快马快,直接比赛

                                                            (2)、田忌最快马比大王最快马慢,让刚才的田忌最慢马与大王最快马比赛

                                                            (3)、田忌最快马与大王最快马相等,让刚才的田忌最慢马与大王最快马比赛

按照这样的策略将所有马都比一次就是田忌能赚最多的钱了,即最优方案。

 

关于贪心策略的证明:(还没想好,等想好再写 标记下###########################################)

 

代码:

/*
acm
hdu:Tian Ji -- The Horse Racing


the test data
2
1 2
1 3
 will let my program produce the WA
 
 
 9
80 140 76 58 93 140 146 74 173
97 80 135 72 0 107 155 66 141

this data also make it be WA

*/
#include<stdio.h>
#include<stdlib.h>

int n=0;

int Tian[1000+5]={0},King[1000+5]={0};

int money=0;

/*compare for the qsort*/
int qcmp(const void * a,const void * b)
{
	int * A=(int *)a;
	int * B=(int *)b;
	return(*B-*A);//from big to small
}

/*for test*/
int test()
{
	return(0);
}

/*main process*/
int MainProc()
{
	while(scanf("%d",&n)!=EOF&&n>0)
	{
		int i=0;
		money=0;
		for(i=0;i<=n-1;i++)
		{
			scanf("%d",&Tian[i]);
		}
		qsort(Tian,n,sizeof(Tian[0]),qcmp);
		for(i=0;i<=n-1;i++)
		{
			scanf("%d",&King[i]);
		}
		qsort(King,n,sizeof(King[0]),qcmp);
		int ti=n-1,ki=n-1,kj=0,tj=0;
		while(ti>=tj)
		{
			if(Tian[ti]>King[ki])
			{
				ki--;
				ti--;
				money+=200;
			}
			else if(Tian[ti]<King[ki])
			{
				money-=200;
				kj++;//the bigest has been matched
				ti--;
			}
			else//if the result is equal it may not come to a match
			{
				if(Tian[tj]>King[kj])
				{
					tj++;
					kj++;
					money+=200;
				}
				else if(Tian[tj]<King[kj])
				{
					if(Tian[ti]<King[kj])
					{
						money-=200;
					}
					ti--;
					kj++;
				}
				else
				{
					if(Tian[ti]<King[kj])
					{
						money-=200;
					}
					ti--;
					kj++;
				}
			}
		}
		printf("%d\n",money);
	}
	return(0);
}

int main()
{
	MainProc();
	return(0);
}


 

 

 

 

 

LCD工程师必看:MIPI DSI的LP/HS模式切换全流程拆解(附高通平台实测) 本文为LCD工程师深度解析MIPI DSI接口中低速(LP)与高速(HS)模式切换的全流程。文章结合高通平台实测波形,从协议状态机、时序关键参数到实战调试案例,系统拆解了模式切换的本质、协同机制及常见问题排查方法,重点阐述了LP模式在低功耗控制与HS模式在高速数据传输中的核心作用与切换时序。 阅读详情

相关推荐

从申请到落地:解锁OAI骨科数据集的实战指南与避坑经验

本文是一份详尽的OAI骨科数据集实战指南,重点解析了从NDA账号申请、权限审批到数据定位与高效下载的全流程。文章分享了使用login.gov注册、Chrome浏览器避坑、nda-tools命令行批量下载等核心技巧,并总结了常见问题排查方法,旨在帮助医学影像与骨关节炎研究者高效解锁这一宝贵数据资源。

weixin_29313547的博客 340

Tian Ji -- The Horse Racing(田忌赛马)/贪心算法

Tian Ji – The Horse Racing(田忌赛马) 田忌赛马的故事 (可以直接看题) 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 hor...

一直努力 1705

《从0到1:CTFer成长之路》书籍配套题目-[第二章 web进阶]文件上传

[第二章 web进阶]文件上传开始行动绕过第1个限制绕过第2个限制 介绍:记录一下笔记,方便以后迅速回忆使用。 《从0到1:CTFer成长之路》书籍配套题目,来源网站:《从0到1:CTFer成长之路》 先看一下题目源码: <?php header("Content-Type:text/html; charset=utf-8"); // 每5分钟会清除一次目录下上传的文件 require_once('pclzip.lib.php'); if(!$_FILES){ echo '

weixin_46703850的博客 3413

Tian Ji -- The Horse Racing

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

chutzpah 389

STM32CubeIDE开发环境搭建与GPIO控制实战

嵌入式开发中,MCU开发环境构建是软硬件协同的起点,其核心在于工具链选型、外设抽象层级与调试可靠性。HAL库作为ST官方标准硬件抽象层,通过统一接口屏蔽芯片差异,显著提升工程可移植性;而STM32CubeIDE则以图形化配置+自动生成代码的方式,实现‘配置即代码’的确定性开发范式。该组合在教学验证、原型迭代和中小规模量产中具备高性价比技术价值,广泛应用于智能传感、工业HMI及IoT终端等场景。本文聚焦STM32CubeIDE 1.15.0环境部署、MyST账户与固件包管理、ST-LINK驱动校验,以及基于H

weixin_33531560的博客 83

【中项】案例分析 | 答题技巧 | 备考 

对偏差原因的分析和对项目未来趋势的理解。如果偏差是由一次性事件或特殊原因引起的,那么这种偏差是非典型的,意味着未来的绩效将回归到预期的绩效水平。例如,如果项目初期的CPI小于1,原因是意外的自然灾害导致成本增加,那么这种偏差被认为是非典型的,因为它不太可能在未来重复发生。例如,如果项目初期的CPI小于1,且原因是低效的团队协作,那么这种偏差被认为是典型的,因为它可能在整个项目生命周期中持续存在。在实际应用中,项目经理通常会结合CPI、SPI和偏差趋势图等工具,综合分析偏差的性质,以制定合理的项目控制策略。

静谧、淡雅 1256

杭电oj1052题:Tian Ji -- The Horse Racing

题目: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 Tia

blingzlh的博客 735

【制作100个unity实战之2】实现一款简单的FPS游戏

你将学到: fps武器脚本 基础瞄准功能 僵尸敌人AI 巡逻范围 攻击间 搜寻半径 追击 枪声吸引周围的僵尸 击中僵直效果

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 3560

Tian Ji -- The Horse Racing(贪心)

Tian Ji – The Horse Racing 题意: 田忌和齐王都有三匹不同类型的赛马,即下等马,中等马和上等马。规则是有三轮比赛,每一匹马只能在一轮中使用。单轮胜者从失败者那里获得两百银元。 因为齐王是齐国最有权势的人,齐王有很好的马,在每类赛马中他的马都比田忌的马好。因此,每次都是齐王赢田忌六百银元。 田忌为此很不高兴,直到他遇见了孙膑,孙膑是中国历史上最有名的军事家之一。由于孙膑使用...

百般回首曾经的相遇,留下的只是一缕残忆 1550

锂电池充放电管理-单片机通用模版

- **①两节锂电池通过电阻分压检测ADC,再根据电压划分电量等级;** - **②充电使用的是锂电池充电IC方案,可通过硬件电阻参数调节充电电流,并可以通过STAT引脚检测充电和满电状态(充电注意会抬高电压);** - **③放电根据ADC_BAT检测电池电量进行等级划分(注意大负载电压骤降瞬间);** - **④充放电过程注意在电量划分等级临界值来回抖动现象;**

a6662580的博客 1492

DAVIS前言:事件相机资料调研

Event-Camera 资料集锦 1. 官网资料 假设了你买了事件相机,就可以在以下几个链接了解相关的参数和gui界面安装,如果不喜欢gui界面安装,可以第2节,安装相关的源码驱动。 DVS/DAVIS产品说明 等相关产品的软硬件介绍。 iniVation DV教程文档 给出在不同平台下的dvs-gui界面,可按照其说明进行相应的操作。 另外,Neuromorphic vision com...

Ian的博客 4512

hdu1052——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): 19020    Accepted Submission(s): 5557 Problem Description H

Alex 1231

15、 深入理解Akka微服务:创建与管理体育博彩平台

本博文深入探讨了如何使用Akka微服务架构为体育博彩平台创建投影和管理事件数据,包括市场投影、投注投影以及数据库投影的实现。同时,介绍了通过gRPC提供HTTP端点以供外部系统访问的方法,并详细说明了在本地环境和Kubernetes中运行应用程序的步骤及配置建议。

weixin_35756624的博客 100

hdu1052-Tian Ji -- The Horse Racing

http://acm.hdu.edu.cn/showproblem.php?pid=1052 这题容易被题目里面所说的二分匹配误导,其实就是利用贪心的思想来做; 下面解释是直接转的,因为我怕我描写的可能不是很清楚,下面那个描写很详细,逻辑也非常缜密 /* HDU 1052 一、如果a的最慢速度大于b的最慢,则直接a的最慢与b的最慢比赛,赢一场; 二、如果a的最慢速度小于b的最慢,则用

bo-jwolf的专栏 959

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

hdu1052 Tian Ji -- The Horse Racing (贪心)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Jav...

3308

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[HDU1052]Tian Ji -- The Horse Racing[贪心]

题目链接:[HDU1052]Tian Ji -- The Horse Racing[贪心] 题意分析:国王和田忌赛马,如果田忌胜利则他将得200银元,如果失败则失去200银元,如果平手,那么一毛也得不到XD。问:怎样使田忌得到的银元最多? 解题思路:首先对两人的赛马进行按速度从小到大排序。从田忌的第一匹马开始,如果这匹马注定失败,那么让这匹马与国王最厉害的马去比赛;如果这匹马并不是注定失败(还

闲停 613

HDU1052——Tian Ji -- The Horse Racing

http://acm.hdu.edu.cn/showproblem.php?pid=1052 这道题用的是贪心策略, 将双方的马按照速度排序,从小到大,最大的先比, 如果能赢就赢,不能的话就比较两个小的,能赢就赢,不能的话就用这个弱马消耗那个速度快的马。 策略就是我要尽量赢,不能赢就拿弱马消耗。 #include<cstring> #include<cm...

aozhe0658的博客 131

HDU1052:Tian Ji -- The Horse Racing

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 an

ACM!荣耀之路! 2334

HDU 1052 Tian Ji -- The Horse Racing

Tian Ji -- The Horse RacingTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4184    Accepted Submission(s): 1100Problem DescriptionHere is

-- bird -- 1577

HDU 1052 Tian Ji -- The Horse Racing

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 othe

u013087645的专栏 909

Hdu1052 Tian Ji -- The Horse Racing

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

Megumin的博客 445

HDU1052Tian 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): 32276    Accepted Submission(s): 9803 Problem Des

←_← 337

HDU 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): 38958    Accepted Submission(s): 11672 Problem Description Here ...

YIU 261

【杭电】HDU 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): 26252 Accepted Submission(s): 7735Problem Description Here is a

楚江枫 665

HDU 1052 Tian Ji -- The Horse Racing

Tian Ji -- The Horse Racing Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Here is a famous story in Chinese history.  "That

※夏日星空※ 321

hdu-1052 Tian Ji -- The Horse Racing

HOT!!! 欢迎参加“金山西山居-2013创意游戏程序挑战赛”! Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13712    Accepted Su

起航 591
上一篇: hdu 1049 Climbing Worm
下一篇: uva 592 Island of Logic
hackerwin7
博客等级 码龄14年 115粉丝 144原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值