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. Chrome设置--disable-web-security解决跨域问题

    这里介绍的是--disable-web-security参数.这个参数可以降低chrome浏览器的安全性,禁用同源策略,利于开发人员本地调试. (1)新建一个chrome快捷方式,右键“属性”,“快捷 ...

  2. 【NOIP2016A组模拟7.13】亚瑟王之宫

    题目 分析 我们定义\(dis_{x,y,x1,y2}\)表示\((x,y)\)到\((x1,y1)\)的距离.这个用spfa求. 接着,枚举两个集合点\((x,y).(x1,y1)\), 得出这两个 ...

  3. (js)粘贴时去掉HTML格式

    一.IE能够触发onbeforepaste事件,因此可以在该事件中直接改变剪贴板中的内容实现过滤效果 二.谷歌由于不能触发onbeforepaste,先阻止默认行为,通过window.getSelec ...

  4. JDK7的新特性

    本篇博客的内容 一.二进制字面量 二.数字字面量可以出现下划线 三.switch 语句可以用字符串 四.泛型简化 五.异常的多个catch合并 六.try-with-resources 语句 一.二进 ...

  5. 微信小程序自定义底部导航栏组件+跳转

    微信小程序本来封装有底部导航栏,但对于想自定义样式和方法的开发者来说,这并不是很好. 参考链接:https://github.com/ljybill/miniprogram-utils/tree/ma ...

  6. 微信小程序支付 java

    原文:https://blog.csdn.net/zhourenfei17/article/details/77765585 话不多说,直接开撸. 支付流程步骤: 1)首先调用wx.login方法获取 ...

  7. 企业级技术解决方案:hbase+es

    1:需求: 解决海量数据的存储,并且能够实现海量数据的秒级查询 Hbase是典型的nosql,是一种构建在HDFS之上的分布式.面向列的存储系统,在需要的时候可以进行实时的大规模数据集的读写操作:但是 ...

  8. loadrunner性能测试巧匠训练营-controller

    1.设置集合点 现在脚本添加集合点的函数,集合点不能添加到事务里面,负责统计事务的时候会把时间计算进去 2.IP欺骗 前言 https://www.cnblogs.com/danbing/p/7459 ...

  9. select标签的下拉框为图片的插件

    1 参考文献: [1] https://github.com/rvera/imag...[2] https://rvera.github.io/image... [3] http://webseman ...

  10. 常见的七种Hadoop和Spark项目案例

    常见的七种Hadoop和Spark项目案例 有一句古老的格言是这样说的,如果你向某人提供你的全部支持和金融支持去做一些不同的和创新的事情,他们最终却会做别人正在做的事情.如比较火爆的Hadoop.Sp ...