Tian Ji -- The Horse Racing

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

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

题意:田忌赛马,知道了田忌的n只马的速度和齐王的n只马的速度,赢一场得到200块,输一场失去200块,平局保持不变。田忌想要更多钱。输出田忌最后最多能拿到多少钱。

题解:把田忌和王的马按速度从大到小排序。之后就是超多的细节。。。。。。。。。具体看代码注释

 #include<bits/stdc++.h>
using namespace std;
int a[],b[];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,t1,t2,k1,k2;
while(~scanf("%d",&n),n)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<n;i++)
{
scanf("%d",&b[i]);
}
sort(a,a+n,cmp);//按速度从大到小排序
sort(b,b+n,cmp);
t1=k1=;//田忌和齐王最强的马的下标
t2=k2=n-;//田忌和齐王最差的马的下标
int ans=;
while(t1<=t2)
{
if(a[t1]>b[k1])//田忌的最强比王的最强更强,
{
ans+=;t1++;k1++;
}
else if(a[t1]<b[k1])//田忌的最强比王的最强弱,用最弱的去比
{
ans-=;t2--;k1++;
}
else//最强的马速度一样
{
if(a[t2]>b[k2])//田忌最慢的马比王最慢的马快
{
ans+=;t2--;k2--;
}
else if(a[t2]==b[k1])//田忌最慢的马和王最快的马速度一样,平局
{
k1++;t2--;
}
else//田忌最慢的马和齐王最快的马比
{
ans-=;k1++;t2--;
}
}
}
printf("%d\n",ans);
}
return ;
}

hdu1052Tian Ji -- The Horse Racing(贪心,细节多)的更多相关文章

  1. HDU1052Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. hdu-1052-Tian Ji -- The Horse Racing(经典)

    /* hdu-1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...

  3. POJ-2287.Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17662   Acc ...

  4. hdu_1052 Tian Ji -- The Horse Racing 贪心

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. HDU 1052 Tian Ji -- The Horse Racing(贪心)

    题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...

  6. UVaLive 3266 Tian Ji -- The Horse Racing (贪心)

    题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...

  7. HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论

    题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...

  8. UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  9. 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. Windows平台JxCore打包

    1.下载JxCore https://raw.githubusercontent.com/jxcore/jxcore-release/master/0311/jx_win64v8.zip 2.解压并配 ...

  2. Sublime 快捷键一览表

    Sublime Text 快捷键一览表 Sublime Text比起Notepad++,UltraEdit之类Code编辑器来说,其功能有过之而无不及,配合着各种插件使用,Sublime Text在实 ...

  3. Ajax跨域问题及解决方案

    目录 复现Ajax跨域问题 Ajax跨域介绍 Ajax跨域解决方案 一. 在服务端添加响应头Access-Control-Allow-Origin 二. 使用JSONP解决 小结 复现Ajax跨域问题 ...

  4. Word中高效输入公式:内嵌公式和Mathtype

    Word中高效输入公式:内嵌公式和Mathtype 前言:对于理工科学生而言,公式输入必不可缺.LaTeX相比Word,在公式输入及排版方面更强大.但是对于轻量级的任务,用Word而言更加轻便(起码不 ...

  5. unittest单元测试框架之测试环境的初始化与还原(fixture)(五)

    1.方法一:针对每条测试用例进行初始化与还原 import unittest from UnittestDemo.mathfunc import * class TestMathFunc(unitte ...

  6. linux下安装rar

    1. sudo wget https://www.rarlab.com/rar/rarlinux-x64-5.5.0.tar.gz 2.解压 tar -zxf rarlinux-5.0.1.tar.g ...

  7. Asp.net 中 OnClientClick 与 OnClick 的区别

    OnClientClick 是客户端事件处理方法,一般采用JavaScript来进行处理,也就是直接在IE端运行,一点击就运行. OnClick 是服务器端事件处理方法,在服务器端也就是IIS中运行, ...

  8. tomcat启动startup.bat一闪而过【亲测有效】

    遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...

  9. mysql碰到的问题总结

    1.问题描述: 连接数据库出现大约10s延迟后才能连接,排除网络问题 解决方案: 通过抓包工具tcpdump抓包分析mysql在连接开始有近10s的空白请求,问题原因就在这 ,不知道在执行什么请求,后 ...

  10. 搭建Apache服务器并使用自签证书实现https访问

    实验环境:两台Centos7.2的虚拟机,一台作CA服务器,一台作Apache服务器,此处安装httpd-2.4.6的版本. 1)CA服务器 # 私钥一般存放位置:/etc/pki/CA/privat ...