uva1344 - Tian Ji -- The Horse Racing 田忌赛马

UVA题解 UVA1344 【Tian Ji -- The Horse Racing】 这题的样例输出有一点小问题,就是要换行,管理大大看到时希望可以改下哈。 QwQ,这是一道双倍经验的题目呢,原题是洛谷的P1650 田忌赛马。 都是一道经典的贪心题目。 将田忌和齐王的马进行排序。 然后将田忌最大的马与国王的进行比较。 如果田忌最大的马大于国王的,那么就胜场加一。 如果田忌最大的马小于国王的,那么就一定会输,所以用田忌最小的马输给国王最大的马。 如果田忌最大的马等于国王的,那么就比较最小的马。 接下来代码也就呼之欲出了。 #include <cmath> #include & 阅读详情

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?
\epsfbox{p3266.eps}

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 integern ( n$ \le$1000) on the first line, which is the number of horses on each side. The nextn integers on the second line are the speeds of Tian's horses. Then the nextn 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


  这个让我想到校赛的时候那个“田忌赛马” ,当时没看清题就当真的田忌赛马来做,结果一直做到比赛结束还没做出来。。。这次是真的。。

  赢一次200块,输一次输200,平局不赔钱。

  开始WA了几次,每次WA了之后都找到了错误的样例,主要是坑在平局的问题上。。正确做法应该是这样:先排序,把田忌最左边的和齐王最左边的比较,能赢就比赛。把田忌最右边的和齐王最右边的比较,能赢就比赛。如果左边右边都不赢,那就那田忌最左边的和齐王最右边的比赛。

  设田忌最左边为A1,最右边为A2,齐王最左边为B1,最右边为B2。

  A1>B1:  那肯定A1去和B1比最划算,赢一把。

  A2>B2:  A2和B2比,赢一把。会不会有可能存在比A2小的也比B2大,用那个更划算?确实可能存在比A2小的也比B2大,这就说明它比最大的都大了,肯定比B2之前的都大,说明它和A2的效果是一样的都能赢齐王剩下的任何马,所以用它和B2比和用A2和B2比是没什么区别的。

  A1=B1,A2=B2: 若A1<A2,也就是A1<B2,过去虽然还亏了200块,但是这样说明B1<A2,拿这两个比的话赚200,等于不赚不亏。若A1=A2,也是不赚不亏。所以这种情况用这个方法也不矛盾。

  A1<B1,A2=B2 : A1反正也是输,输给B2或许A2还能赢B1。

  A1=B1,A2<B2: 已经没有马能打过B2了,必输一次,就拿最垃圾的马过去,不要浪费了好马=。=

  A1<B1,A2<B2 : 同上,反正怎样都要输给B2。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define MAXM 60
#define eps 1e-9
#define pii pair<int,int>
using namespace std;
int N,a[MAXN],b[MAXN],B[MAXN];
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N),N){
        for(int i=0;i<N;i++) scanf("%d",&a[i]);
        for(int i=0;i<N;i++) scanf("%d",&b[i]);
        sort(a,a+N);
        sort(b,b+N);
        int tp1=0,tp2=N-1,gp1=0,gp2=N-1,win=0;
        while(tp1<=tp2){
            if(a[tp1]>b[gp1]){
                win++;
                tp1++;
                gp1++;
            }
            else if(a[tp2]>b[gp2]){
                win++;
                tp2--;
                gp2--;
            }
            else{
                if(a[tp1]<b[gp2]) win--;
                tp1++;
                gp2--;
            }
        }
        printf("%d\n",win*200);
    }
    return 0;
}


RTKLIB PPP与RTK_POST精密定位实战:从实时到事后处理的完整流程解析 本文详细解析了如何使用RTKLIB进行精密单点定位(PPP)的完整流程,涵盖从实时处理到事后处理(RTK_POST)的关键步骤。文章深入探讨了数据准备、软件配置、核心参数设置(如电离层、对流层、潮汐改正)以及常见问题排查,旨在帮助用户掌握高精度GNSS定位技术,实现从入门到精通的实战应用。 阅读详情

相关推荐

混合整数规划(MIP)在资源优化中的实战应用

本文深入探讨了混合整数规划(MIP)在资源优化中的实战应用,从菜市场采购到工厂调度,再到物流配送和钢铁厂切割问题。通过分支定界法和割平面法等技术,MIP能够有效解决包含整数和连续变量的复杂优化问题,提升企业运营效率。文章还介绍了现代求解器的优化策略和实际项目中的避坑指南,帮助读者更好地应用MIP技术。

weixin_30500105的博客 99

Tian Ji -- The Horse Racing(贪心)

Tian Ji – The Horse Racing 题意: 田忌和齐王都有三匹不同类型的赛马,即下等马,中等马和上等马。规则是有三轮比赛,每一匹马只能在一轮中使用。单轮胜者从失败者那里获得两百银元。 因为齐王是齐国最有权势的人,齐王有很好的马,在每类赛马中他的马都比田忌的马好。因此,每次都是齐王赢田忌六百银元。 田忌为此很不高兴,直到他遇见了孙膑,孙膑是中国历史上最有名的军事家之一。由于孙膑使用...

百般回首曾经的相遇,留下的只是一缕残忆 1552

UE4-(反射)场景采集之场景采集2D

一、场景采集分为两种:场景采集2D、场景采集立方体 场景采集2D:采集2D图片模式。 操作: 1.选择场景采集2D,是一个摄像机的物体,摆放到场景中。 2.在文件夹空白处创建纹理,一定要注意创建纹理的格式, 纹理分为立方体渲染目标和渲染目标,此处我们使用的是2D模式,所以选择渲染目标(Texture Render Target2D)。创建后重命名为RenderTextue2D。 3.选择场景中刚拖入的 SceneCapture2D 摄像机在细节面板的Scene Capture 将..

Stone的博客 4607

Tian Ji -- The Horse Racing

G - Tian Ji – The Horse Racing 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 hav

weixin_52879528的博客 441

大模型备案避坑指南:如何设计符合TC260标准的评估测试题集?

本文深入探讨了如何设计符合TC260标准的大模型评估测试题集,以顺利通过备案。文章从解构标准核心逻辑出发,详细阐述了题集设计的工程化流程、核心原则与常见误区,并提供了模拟真实对抗的高阶测试策略与度量分析方法,旨在帮助企业构建一套能真实反映模型安全水位、有效规避风险的评估体系。

kafka6courier的博客 673

安全与加密常识(10)linux系统证书管理

1. 将自定义CA证书(如my-ca.crt)放到源目录# 2. 编辑配置文件,启用该证书(末尾添加一行 my-ca.crt)# 3. 执行命令同步(核心工作环节)# 4. 验证结果:检查是否生成哈希链接和合并到证书束ls /etc/ssl/certs/my-ca.pem # 证书文件ls /etc/ssl/certs/$(openssl x509 -hash -noout -in /usr/share/ca-certificates/my-ca.crt).0 # 哈希链接。

二进制君 311

Tian Ji -- The Horse Racing(田忌赛马)

问题描述 这里有一个中国历史上著名的故事。 “那是2300年前的事了。田基将军是齐国的高官。他喜欢和国王和其他人一起赛马。” “天王两人都有三匹不同等级的马,分别是普通马、加号马和超级马。规则是一场比赛分三轮,每匹马必须用一轮。一轮胜出者从失败者手中夺走两百银元。” 国王是全国最有权势的人,他的马非常好,每一级的马都比田...

weixin_45323349的博客 1382

【Claude】权限被拒绝 (Permission Denied) 全场景排查 — 已解决

权限配置是 Claude Code 安全使用的核心机制。通过合理的 allow/deny 规则、正确的通配符使用、子代理独立配置、定期安全审计,可以在保证安全性的同时不影响开发效率。deny 优先:deny 规则优先级高于 allow,先保护敏感资源通配符精确单层匹配,**递归匹配,Bash(cmd*)允许带参数最小权限:只允许必要的操作,避免Bash(*)等危险规则CI 用 skip:Headless 模式用避免挂起子代理独立:每个子代理单独配置权限,不自动继承保护密钥:deny.env。

一键难忘的博客 3988

田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法

这两天碰到一道看似很简单,但是实际做起来确实比较难的问题,在这里分析讨论一下。 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth

lawrencesgj的专栏 1万+

GB28181 —— 3、C++编写GB28181设备端,完成注册、注销、保活心跳、响应查询目录等功能(附源码)

GB28181 —— Win10下编写GB28181设备端,完成注册、注销、保活心跳、响应查询目录等功能(附源码)

1320

【法如faro】三维激光软件Scene2023数据处理(自动配准并转换坐标)流程

法如faro Scene2023数据处理(自动配准并转换坐标)的主要流程为:新建项目、导入数据、处理、自动注册、坐标系转换、模型导出立和面模型导出等。

智慧空间实验室 2408

UVa 1344 Tian Ji The Horse Racing

本文基于田忌赛马的历史故事,提出一个通过贪心算法优化比赛策略的问题。双方各有n匹马,每匹马有不同的速度值,比赛规则为n轮对决,每匹马上场一次,胜者得200银元,败者失200银元,平局无得失。文章分析了将问题转化为二分图最大权匹配的可能性,但提出了更高效的贪心解法:将双方马匹按速度从大到小排序后,通过比较最快和最慢的马来决定出场策略,以最大化收益。算法时间复杂度为O(n log n),适用于n≤1000的情况。文中还提供了C++代码实现,详细说明了排序、指针移动和胜负判断等关键步骤。该贪心算法在每一步做出局部

寂静山林 412

(三十九)Flask 多应用架构(Multiple Apps)与 DispatcherMiddleware 实战解析

(三十九)Flask 多应用架构(Multiple Apps)与 DispatcherMiddleware 实战解析

孤寒者的博客 1万+

uva 1344 Tian Ji The Horse Racing

题目链接: 点击打开链接 题目大意: 田忌和国王赛马,求最多能赢得的钱数 思路: 贪心 分析:  1. 把双方的马按速度从小到大排列。 2. 从最慢的比起,设田忌速度为t,国王速度为k。如果ti ki,比,以最小的代价赢下一局。 3. 如果ti = ki,这时要比较双方的最快马速,设为tm和km: a 如果tm b 如果tm > km,最快的

kthsdwwl 898

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

UVa 1344 Tian Ji -- The Horse Racing 解题报告(贪心)

1344 - Tian Ji -- The Horse Racing Time limit: 3.000 seconds

SF-_- 的 ACM 博客 998

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Tian Ji -- The Horse Racing UVA - 1344 贪心

问题 https://vjudge.net/problem/UVA-1344 分析 开始的想法是,马匹的速度从小到大排序,对于齐王的每一匹马,田忌选择刚好能够赢过这匹马的马,但是无法处理相等的情况,有的时候需要错位,有的时候不能错位 例子: 3 10 20 30 10 20 30 3 10 20 30 10 19 29 参考:https://blog.csdn.net/qwsin/article/...

zpf1998的博客 178

uva 1344 Tian Ji -- The Horse Racing

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4090 思路: 分别把田忌和国王的马按速度从小到达排序,然后开始从第一个比较,也就是田忌中最小的马和国王最小的马比较,速度分别设为s1, s2,如果s1s2,那么反正这匹马都是输,就让这匹马

D_Double's Journey 1360

UVA 1344 Tian Ji -- The Horse Racing

Tian Ji -- The Horse Racing 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 wit

Remilia's 1678

uva--1344--Tian Ji -- The Horse Racing

题目大意: /************ *题目大意:规则类似田忌赛马 *给出齐王每匹马的速度和田忌每匹马的速度 *田忌每赢一次就获得200两银子 *问田忌最多能赢得多少银子 ************/ 解题思路:把双方的马都排序,升序降序无所谓,然后YY了一下,田忌能赢最多钱的方案应该是排好序之后的序列往左或者往右(视排列顺序而定,假设是升序)移动确定的位之后的比赛序列,所以试了一下

WoniuGe 700

UVA1344 - Tian Ji -- The Horse Racing

题意:田忌赛马,求出能赢的最大的钱数 思路: #include #include #include #include using namespace std; const int N = 1005; int a[N], b[N]; int main() { int n; while (scanf("%d", &n) && n) { for

懒人一枚。 647

UVa 1344 - Tian Ji -- The Horse Racing(贪心)

给出田忌和齐威王的马的属性,求田忌最多能赢多少钱。 类似于故事里的方法,排序后田忌最差的马若跑不过齐王最差的马,就和最好的马跑。最好的马优先与能比过的最好的马跑。

wcr1996的专栏 1700

UVA 1344 - Tian Ji -- The Horse Racing(贪心)

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 T

Remilia's 1248

动态规划 UVA 1344 - Tian Ji -- The Horse Racing

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 T

We Can 762

UVa 1344 - Tian Ji -- The Horse Racing

题意 田忌赛马,求最好的结果。 思路 乍一看挺简单的,但是需要考虑的还是挺多的。 如果田鸡最快的比齐王最快的快,直接比下去。 如果田鸡最快的比齐王慢,这时候就要用最慢的赖一下齐王。但是,不能马上用最慢的去赖,因为可能田鸡最慢的马还比齐王最慢的马要快那么一丢丢,如果用这只马去赖就太亏了,他还有利用价值。这时候就顺便用这只马赢一下齐王最慢的马,然后接着比较

一只冰熊 749

POJ2287 HDU1052 UVA1344 UVALive3266 ZOJ2397 Tian Ji -- The Horse Racing【贪心】

Tian Ji -- The Horse RacingTime Limit: 5000MS Memory Limit: 65536KTotal Submissions: 15776 Accepted: 4864DescriptionHere is a famous story in Chinese history. That was about 2300 years ago. General Ti...

海岛Blog 1501
上一篇: uva1450 - Airport
下一篇: uva1467 - Installation 贪心+暴力
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址