POJ-2287.Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 17662 | Accepted: 5452 |
Description
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
Output
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 (贪心)的更多相关文章
- POJ 2287 Tian Ji -- The Horse Racing(贪心)
题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
/* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...
- 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 ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- UVaLive 3266 Tian Ji -- The Horse Racing (贪心)
题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...
- HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论
题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...
- UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
- 【贪心】[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 ...
随机推荐
- 七、ARM 指令集
7.1 数据处理指令 7.1.1 数据传送类 MOV 类指令:核内寄存器间的数据传送 加载和存储指令(L/S):核内寄存器与挂在存储器总线上器件的数据传送 注意: 核内寄存器就是 R0-R15 外设寄 ...
- 基于iview使用jsx扩展成可编辑的表格
<template> <div> <Table :columns="columns" :data="data"></T ...
- USACO Overplanting ( 线段树扫描线 )
题意 : 在二维平面上给出 N 个矩形,问你所有矩形构成的图案的面积是多少(相互覆盖的地方只计算一次) 分析 : 求矩形面积并可以模拟来做,不过使用线段树来辅助做扫描线可以更高效地求解 扫描线顾名思 ...
- 贪心整理&一本通1431:钓鱼题解
题目传送 (其实有一个更正经的题解) 看了许久,发现这题貌似就是一个动态规划啊,但毕竟是贪心题库里的题,还是想想用贪心解吧. 经过(借鉴大佬思路)十分复杂的思考后,终于理解出了这题的贪心思路.该题的难 ...
- 通过运行窗口输入命令方式,打开Internet窗口
WIN + R 按键, 运行窗口出来后,我们输入‘inetcpl.cpl“,然后按确定. 弹出Interent属性窗口.
- http协议的深刻理解
https://www.cnblogs.com/mayite/p/9095986.html
- Codeforces Round #603 (Div. 2) E. Editor
E. Editor 题目链接: https://codeforces.com/contest/1263/problem/E 题目大意: 输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ ...
- es之分词器和分析器
Elasticsearch这种全文搜索引擎,会用某种算法对建立的文档进行分析,从文档中提取出有效信息(Token) 对于es来说,有内置的分析器(Analyzer)和分词器(Tokenizer) 1: ...
- [CSP-S模拟测试]:飘雪圣域(莫队)
题目描述 $IcePrincess\text{_}1968$和$IcePrince\text{_}1968$长大了,他们开始协助国王$IceKing\text{_}1968$管理国内事物. $IceP ...
- 高通Camera驱动分析【转】
本文转载自:http://blog.csdn.net/liwei16611/article/details/53955711 1.Sensor slave配置 结构体msm_camera_sensor ...