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 ...
随机推荐
- php 获取准确的ip,并通过ip准确获取所在区域 的方法
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- 【串线篇】spring boot嵌入式Servlet容器启动原理;
什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat: 获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行run方法 2).r ...
- weighted choice in python
对列表按概率采样 Input: a collection C of elements and a probability distribution p over C; Output: an eleme ...
- CSS3文本阴影、边框阴影
CSS3添加阴影 一.使用text-shadow属性为文本添加阴影 二.使用box-shadow属性为边框添加阴影 一.为文本添加阴影 text-shadow 使用text-shadow,可以 ...
- 怎么实现超大文件上传 2-3GB
1 背景 用户本地有一份txt或者csv文件,无论是从业务数据库导出.还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工.挖掘和共创应用的时候,首先要将本地文件上传至ODPS,普通的小文件通 ...
- luoguP1186 玛丽卡 x
P1186 玛丽卡 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道 ...
- Laya 首日红点逻辑
Laya 首日红点逻辑 @author ixenos 2019-08-26 10:50:27 1.原理:显然,首日红点意味着包含进程销毁的情况,那么就要持久化存储信息,这里我们使用LocalStora ...
- C. Almost Equal
C. Almost Equal n个数字全排成一个圈,满足任意相邻n个之和之间最大最小值之差不超过1 n为偶数时 不存在 n为奇数,构造 #include<bits/stdc++.h> u ...
- 登陆一个系统时,前端js实现的验证,记住密码等功能
记住密码部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- 【MySQL】 知识点记录
0. 定位和排查问题的常用语句 查询 正在执行的事务(这个输出有事物状态表明是否等待锁):SELECT * FROM information_schema.INNODB_TRX 查看正在锁的事务:SE ...