hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing

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

Total Submission(s): 18155    Accepted Submission(s): 5281

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

题意:给出多匹马的情况,求出能使自己利益最大化的钱数。

思路:贪心算法,首先对每个人的马,进行 排序,让田忌的最快的马,与齐王最快的马作比较,要是田忌的马慢,用最慢的马与齐王最快的马比赛。

代码如下:

<span style="font-size:14px;">#include<stdio.h>
#include<algorithm>
using namespace std;
int a[10010],b[10010];
int main()
{
	int n,i,i2,j2,j,count,sum;
	while(~scanf("%d",&n),n)
	{
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		scanf("%d",&b[i]);
		sort(a,a+n);
		sort(b,b+n);
		int count=0,i=n-1,j=n-1,i2=0,j2=0,sum=0;
		while(count++<n)
		{
			if(a[i]<b[j])
			{
				sum-=200;
				i2++;//i不变,j改变接着与齐王后面的马比较,要是能赢田忌再换稍微慢点的马
				j--;
			}
			else if(a[i]>b[j])
			{
				sum+=200;
				i--;j--;
			}
			else
			{
				if(a[i2]>b[j2])
				{
					sum+=200;
					i2++;
					j2++;
				}
				else
			    {
			    	if(a[i2]<b[j])//用最慢的马与齐王最快的马比较
			    	{
			    		sum-=200;
			    		i2++;
			    		j--;
			    	}
			    }
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}</span>

hdu 1052(田忌赛马 贪心算法,sort排序)

时间: 2024-10-09 08:42:47

hdu 1052(田忌赛马 贪心算法,sort排序)的相关文章

hdu 4296 Buildings 贪心算法 今日首A 详细解析 ,有些数据类型最好用long long

Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2278    Accepted Submission(s): 870 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr

STL源代码分析——STL算法sort排序算法

前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SGI STL中的排序算法不是简单的高速排序,而是交叉利用各种排序:堆排序.插入排序和高速排序:这样做的目的是提高效率.针对数据量比較大的採用高速排序,数据量比較小的能够採用堆排序或插入排序. 本文介绍了有关排序的算法random_shuffle.partition.stable_partition.sort.s

hdu 1052 田忌赛马

贪心,排序从大到小.. 先比大的.跑只是就拿最小的来送死.. , 假设是平局就比后面的... 若后面也是平局就拿去跟前面的去跑. .. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int s[1005],w[1005]; int main() { int n,i; while(scanf("%d&qu

hdu 3466 Proud Merchants &lt;背包+sort排序&gt;

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3616    Accepted Submission(s): 1511 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

HDU 4950 Monster (贪心算法)

题目链接 题意:有一个怪物的血量为h,人攻击怪物,每回合可以杀掉a滴血,再回b滴血,k个回合之后人会休息一回合,即人不攻击而怪物回b滴血,问能否杀死.翻译过来就是给定一个数h,每轮可以先减a再加b,k轮后会有一轮只加b不减a,如果再这过程中存在h<=0就输出YES,否则输出NO. 题解:注意是先减a再加b,有可能不加b就已经h<=0,先分类讨论ab,如果a比b大那么只有可能第一回合杀死,一旦+b就不可能在杀死了,如果a比b小,讨论在经过k-1轮后第k轮只减不加能否<=0,再讨论k+1轮的

贪心算法?我全都要!——算法第四章上机实践报告

算法第四章上机实践报告 一.        实践题目 4-1 程序存储问题 (90 分) 设有n 个程序{1,2,…, n }要存放在长度为L的磁带上.程序i存放在磁带上的长度是 li,1≤i≤n. 程序存储问题要求确定这n 个程序在磁带上的一个存储方案, 使得能够在磁带上存储尽可能多的程序. 对于给定的n个程序存放在磁带上的长度,计算磁带上最多可以存储的程序数. 输入格式: 第一行是2 个正整数,分别表示文件个数n和磁带的长度L.接下来的1行中,有n个正整数,表示程序存放在磁带上的长度. 输出

【算法】贪心算法_节目时间安排问题

问题描述 “今年暑假不学习?” “是吗?那你打算干什么呢?” “看电视剧呀!” “那么多电视剧你看得完吗?” "对哦,那是的好好安排一下节目了." 确实如此,暑假来了,假期档的电视剧也来了,估计很多电视迷会抛开学业,奔向电视. 作为电视迷,一定想看在一天内看尽量多的完整的电视剧.当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事).流淌的美好时光.长安十二时辰.陈情令,以及王小丫的<开心辞典>等等,假设你已经知道了所有你喜欢看的电视

C语言贪心(2)___田忌赛马(Hdu 1052)

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 t

HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 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