1052 Tian Ji -- The Horse Racing

题目详情: 

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47942    Accepted Submission(s): 14651

Problem 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
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1051 1045 1800 1053 1789 
  

题目大意:

田忌和国王赛马看谁的马快,匹数均为n,田忌每赢国王一局,+200元,输一局-200元,平局+0元,

问如何安排比赛马的出场比赛顺序可以使田忌尽可能多赢钱。

 解题思路:

先把田忌的马和国王的马分别降序排序,如果田忌的快马比国王的快马快,则胜局+1,否则,

拿田忌的慢马和国王的慢马比较,慢马能赢就赢,如果田忌的慢马输了再拿田忌的慢马和国王的快马进行比较,这里贪心比较明显,用自己最慢的马去浪费对方最快的马,反正走到这步肯定没有马可以跑赢对面的快马,干脆拿慢马去消耗掉一个比赛的机会。

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int n;//每次双方各有多少匹马
    int horse;//马(用于输入)
    while(cin>>n&&n!=0){
        vector<int> tianji;//田忌的马
        vector<int> king;//皇帝的马
        int sum_win=0;//田忌赢的场数
        int tianji_fast=0;//田忌的快马
        int tianji_slow=n-1;//田忌的慢马
        int king_fast=0;//国王的快马
        int king_slow=n-1;//国王的慢马
        for(int i=0;i<n;++i){
            cin>>horse;
            tianji.push_back(horse);
        }
        for(int i=0;i<n;++i){
            cin>>horse;
            king.push_back(horse);
        }
        sort(tianji.begin(),tianji.end(),cmp);//降序排序
        sort(king.begin(),king.end(),cmp);//降序排徐
        for(int i=0;i<n;++i){
            if(tianji[tianji_fast]>king[king_fast]){//田忌的快马赢
                ++tianji_fast;//田忌下一匹快马
                ++king_fast;//国王下一匹快马
                ++sum_win;//赢的次数+1
            }
            //执行到这说明田忌的快马没有国王的快马快
            else if(tianji[tianji_slow]>king[king_slow]){//田忌的慢马比国王的慢马快
                --tianji_slow;//田忌的上一匹慢马
                --king_slow;//国王的上一匹慢马
                ++sum_win;//赢的次数+1
            }
            //执行到这说明田忌的慢马没有国王的慢马快
            else if(tianji[tianji_slow]<king[king_fast]){//田忌的慢马比国王的快马慢
                --tianji_slow;//田忌的上一匹慢马
                ++king_fast;//国王的下一匹快马
                --sum_win;//输了,赢的次数-1
            }
        }
        cout<<sum_win*200<<endl;//输出
    }
    return 0;
}

 推荐几款学习编程的网站

免费在线开发平台(https://docs.ltpp.vip/LTPP/

        探索编程世界的新天地,为学生和开发者精心打造的编程平台,现已盛大开启!这个平台汇集了近4000道精心设计的编程题目,覆盖了C、C++、JavaScript、TypeScript、Go、Rust、PHP、Java、Ruby、Python3以及C#等众多编程语言,为您的编程学习之旅提供了一个全面而丰富的实践环境。

        在这里,您不仅可以查看自己的代码记录,还能轻松地在云端保存和运行代码,让编程变得更加便捷。平台还提供了私聊和群聊功能,让您可以与同行们无障碍交流,分享文件,共同进步。不仅如此,您还可以通过阅读文章、参与问答板块和在线商店,进一步拓展您的知识边界。

        为了提升您的编程技能,平台还设有每日一题、精选题单以及激动人心的编程竞赛,这些都是备考编程考试的绝佳资源。更令人兴奋的是,您还可以自定义系统UI,选择视频或图片作为背景,打造一个完全个性化的编码环境,让您的编程之旅既有趣又充满挑战。

免费公益服务器(https://docs.ltpp.vip/LTPP-SHARE/linux.html

        作为开发者或学生,您是否为搭建和维护编程环境而困扰?现在,有一款免费的公共服务器,内置多种编程语言的编程环境,并且配备了在线版VS Code。让您可以随时随地在线写代码,无需复杂配置,专注于开发和学习。(PS:毕竟是免费公共的服务器,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费公益MYSQL(https://docs.ltpp.vip/LTPP-SHARE/mysql.html

        作为一名开发者或学生,您是否常常为数据库环境的搭建而烦恼?是否因为预算有限而无法使用高性能的数据库服务?现在,有一款免费的MySQL服务器,专为开发者和学生量身打造,让你轻松无忧地进行开发和学习!内置在线phpmyadmin管理面板,方便用户查看数据。(PS:毕竟是免费公共的MYSQL,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费在线WEB代码编辑器(https://docs.ltpp.vip/LTPP-WEB-IDE/

        无论你是开发者还是学生,编程环境的搭建和管理可能会占用你宝贵的时间和精力。现在,有一款强大的免费在线代码编辑器,支持多种编程语言,让您可以随时随地编写、调试和运行代码,提升编程效率,专注于创意和开发。

免费二维码生成器(https://docs.ltpp.vip/LTPP-QRCODE/

        无论是企业宣传、活动推广,还是个人信息分享,二维码都是一种快速、高效的信息传递方式。现在,有一款功能强大的二维码生成器,不仅易于使用,还具备多种便捷功能,帮助您更轻松地生成和管理二维码。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WA-自动机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值