Tian Ji -- The Horse Racing

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17662   Accepted: 5452

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

 
思路:孙膑说:“现在用您的下等马对付他们的上等马,用您的上等马对付他们的中等马,用您的中等马对付他们的下等马。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; const int maxn = + ;
int n, yy[maxn], zz[maxn]; int main() {
while(cin >> n && n) {
for(int i = ; i < n; i ++) {
cin >> yy[i];
}
for(int i = ; i < n; i ++) {
cin >> zz[i];
}
sort(yy, yy + n);
sort(zz, zz + n);
int lowyy = , lowzz = , highyy = n - , highzz = n - , cnt1 = , cnt2 = ;
while(highyy >= lowyy) {
if(yy[lowyy] > zz[lowzz]) {//打得过就赚大了
lowyy ++;
lowzz ++;
cnt1 ++;
} else if(yy[lowyy] < zz[lowzz]) {//打不过就用最垃圾的打他最牛皮的
lowyy ++;
highzz --;
cnt2 ++;
} else {//平局
if(yy[highyy] > zz[highzz]) {//平局就看我最牛皮的能不能打过你最牛皮的,打的过就让他们两个先打
highyy --;
highzz --;
cnt1 ++;
} else {//打不过就只能让我最垃圾的打你最牛批的
if(yy[lowyy] < zz[highzz]) cnt2 ++;
lowyy ++;//为什么不考虑两个最牛皮的能不能打平手?我的最牛皮的留着打你稍微弱一点的
highzz --;
}
}
}
int ans = cnt1 - cnt2;
cout << ans * << endl;
// cout << cnt1 << endl << cnt2 << endl;
}
return ;
}

POJ-2287.Tian Ji -- The Horse Racing (贪心)的更多相关文章

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

    题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...

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

    /* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...

  3. 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 ...

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

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

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

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

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

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

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

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

  8. 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...

  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. springcloud 配置actuator

    pom.xml <!--Spring Boot Actuator,感应服务端变化--> <dependency> <groupId>org.springframew ...

  2. python如何导入自定义文件和模块$PYTHONHOME$\Lib\site-packages 方法

    python 中如何引用自己创建的源文件(*.py)呢? 也就是所谓的模块. 假如,你有一个自定义的源文件,文件名:saySomething.py .里面有个函数,函数名:sayHello.如下图: ...

  3. Python---Tkinter---贪吃蛇

    # 项目分析: - 构成: - 蛇  Snake - 食物 Food - 世界 World - 蛇和食物属于整个世界 class World: self.snake self.food ------- ...

  4. 详述 DB2 分页查询及 Java 实现的示例_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 博主说:有时候,我们需要对数据库中现有的数据进行大量处理操作(例如表中的某个字段需要全部更新等),如果直接使用selec ...

  5. TweenMax的GSAP(GreenSock动画平台)GSAP,专业的Web动画库

    很好奇红框框里面的内容是什么,于是点了进去,又百度了下这个英文缩写具体指的什么东西. GSAP的全名是GreenSock Animation Platform,是一个从flash时代一直发展到今天的专 ...

  6. Day01_初识Python

    Python简介 Python的历史 1.1989年圣诞节:Guidao von Rossum开始写Python语言的编译器 2.1991年2月:第一个Python编译器(同时也是解释器)诞生,他是使 ...

  7. 认识.net Framework

  8. 字典树模板( 指针版 && 数组版 )

    模板 :  #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...

  9. Java——常用类(基础类型数据包装类)

    [包装类]   包装类(如Integer.Double等)这些类封装了一个相应的基础数据类型数值,并为其提供了一系列操作.     例如:java.lang.Integer类提供了以下构造方法:   ...

  10. redis过期策略设置

    中6中过期策略的具体方式. redis 中的默认的过期策略是volatile-lru .设置方式 可以通过命令直接设置 config set maxmemory-policy volatile-lru ...