田忌赛马
来源:互联网 发布:三联送货单打印软件 编辑:程序博客网 时间:2024/05/18 05:47
田忌赛马
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
- 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.- 输入
- The input consists of many 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.
- 输出
- For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
- 样例输入
392 83 7195 87 74220 2020 20220 1922 18
- 样例输出
20000
解题思路:贪心思想,是借鉴大牛的思路, 就是尽量用自己的弱马把对方强马拉下马, 为自己的强马留下充足的机会去赢(说白了弱马就是炮灰), 当然前提是自己的弱马比不上对方的弱马, 如果比得上当让去赢了(白赢谁不赢)【就是尽量用自己的弱马赢得比赛】。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;int tian[1005], king[1005];bool cmp(int a, int b){ return a < b;}int main(){ int count, i, n, t_slow, t_fast, k_slow, k_fast; //田忌赢的局数 临时变量 马匹数 田忌慢马 快马 齐威王慢马 快马 while(scanf("%d", &n) != EOF){count = 0;for(i = 0; i < n; i++){scanf("%d", &tian[i]);} for(i = 0; i < n; i++){scanf("%d", &king[i]);} sort(tian, tian+n, cmp);sort(king, king+n, cmp);t_slow = k_slow = 0; //初始化t_fast = k_fast = n-1;while(t_slow <= t_fast){if(tian[t_slow] > king[k_slow]) //从慢的开始比赢了记一局{count++;t_slow++;k_slow++;}else if(tian[t_slow] < king[k_slow]) //输了就把对方最快的拉下马{count--;t_slow++;k_fast--;}else //平局是下下策 将对方的最快马拉下马才是王道{if(tian[t_fast] > king[k_fast]) //若自己的快马比对方的快马快就直接记一局, 直到出现自己的快马比对方快马慢时用自己的慢马把对方快马拉下马{count++;t_fast--;k_fast--;}else {if(tian[t_slow] < king[k_fast]) //若还剩最后一匹马而且和对方相等, 则count 不--{count--;}t_slow++;k_fast--;}}}printf("%d\n", count*200); //统计结果} return 0;}
- 田忌赛马
- 田忌赛马
- 田忌赛马
- 田忌赛马
- nyoj364(田忌赛马)
- 田忌赛马
- nyoj364田忌赛马
- HDOJ1052田忌赛马
- acm-田忌赛马
- 田忌赛马
- 田忌赛马
- NYOJ364 田忌赛马
- nyoj364田忌赛马
- LA3266田忌赛马
- 田忌赛马
- 田忌赛马
- 田忌赛马
- poj2287 田忌赛马
- 黑马程序员_面向对象
- 获得ORACLE表的自增长主键
- 将ContOS默认语言改成英语
- ashx中使用session[解决方案]
- 如何拿到it公司offer
- 田忌赛马
- FTP和TFTP区别
- 使用dom4j来解析相关的xml字符串
- 使用Gson将Java对象转换为JSON
- git remote使用
- uva 11198 Dancing Digits
- Linux/Unix笔记本
- python 连接sql server
- C++线性方程求解