2016SDAU课程练习一1002_here is a famous story in chinese history. that wa-程序员宅基地

技术标签: 贪心算法  

Problem C 


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

题意:就是田忌赛马的萌萌历史小故事


思路:这个思路貌似从小看故事里就有吧,就是看马的好坏,要是田忌最差的马比王最差的马差那就用田忌最差的和王最好的比,不然就最差和最差比,田忌赢了加200,输了减200,求最多金子数,思路比较好想


感想:一开始完全想暴力一点挨个比,后来发现不用,借鉴了下别人的,其实思路差不多,但是我不太会表示

这个题应该分这几种去讨论:
1. 田忌慢马 > 齐王慢马 win ++;
2. 田忌慢马 < 齐王慢马 lose ++ ,齐王快马 out;
3. 田忌慢马 = 齐王慢马
{
          if(田忌快马 > 齐王快马) win ++ ;
           else lose ++ 齐王快马 out;
}

AC代码:


#include <cstdio>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,i,j,w,l;
    int a[1000];
    int b[1000];
    int t[1000];
    while(cin>>n)
    {
        if(n==0) break;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<n;i++)
        {
            cin>>b[i];
        }
        sort(a,a+n,cmp);
        sort(b,b+n,cmp);
        w=l=0;
        int tm,tn,qm,qn;
        tm=qm=0;tn=qn=n-1;
        while(tm<=tn)
        {
            if(a[tn]>b[qn])
            {
                w++;
                tn--;
                qn--;
            }
            else if(a[tn]<b[qn])
            {
                l++;
                tn--;
                qm++;
            }
            else
            {
                if(a[tm]>b[qm])
                {
                    w++;
                    tm++;
                    qm++;
                }
                else
                {
                    if(a[tn]<b[qm])
                        l++;
                    tn--;
                    qm++;
                }
            }
        }
        cout<<200*(w-l)<<endl;
    }
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34264704/article/details/50949605

智能推荐

不属于python标准库的是_python标准库和扩展库-程序员宅基地

文章浏览阅读1k次。Tkinter————Python默认的图形界面接口。Tkinter是一个和Tk接口的模块,Tkinter库提供了对Tk API的接口,它属于Tcl/Tk的GUI工具组。Tcl/Tk是由John Ousterhout发展的书写和图形设备。Tcl(工具命令语言)是个宏语言,用于简化shell下复杂程序的开发,Tk工具包是和Tcl一起开发的,目的是为了简化用户接口的设计过程。Tk工具包由许多不同的小部..._不属于python的标准库

echarts官方世界地图json-程序员宅基地

文章浏览阅读1.4k次。发现现有的公开资源中关于世界地图的json很难找到,要么找到之后并不是通过经纬度定义的边界线,后来偶然发现在echarts的官方文档中存在世界地图的json,现记录一下。_世界地图json

Python 统计一行字符中单词的个数_python上手--基本语法和数据类型基础-程序员宅基地

文章浏览阅读3.6k次。前面介绍了选择python开发工具的方法,根据需求和学习阶段的不同来选择不同的编译器。从本篇开始我们就选用Anaconda中的spyder模块作为开发编译平台,在下载过程中可以直接百度搜索anaconda,就可以进入其官网,找到下载链接,进行下载。Anaconda Python/R Distribution - Free Download​www.anaconda.com这里建议选择python3..._统计一行文本的单词个数python

业务规则引擎_使用业务规则作为授权引擎-程序员宅基地

文章浏览阅读515次。成绩单 在本文中,您将学习如何使用Nools业务规则引擎在Node.js应用程序中做出授权决策。 这样做使您可以更改应用程序的授权策略,而无需更改源代码,从而使更新此类策略更容易。 构建应用程序所需的条件 一个Bluemix帐户 HTML,JavaScript和MEAN Web应用程序堆栈知识 可以将Node.js应用程序上载到Bluemix的开发环..._nools

boost/thread/pthread/thread_data.hpp:143: undefined reference to `vtable for boost::detail::thread_d_undefined reference to boost::detail::thread_da-程序员宅基地

文章浏览阅读1.6k次。编译出现这样的问题:/home/minbo/git_root/XChat/chat_room/nroom_manager_server/test/../../../common/build/libcommon.a(RabbitmqProducerAsync.cpp.o): In function `boost::detail::thread_data_base::thread_data_base(_undefined reference to boost::detail::thread_da

SQL统计所有表的行数_sql 表行数-程序员宅基地

文章浏览阅读1.5k次。SQL统计所有表的行数_sql 表行数

随便推点

将yolov5的detect.py改写成可以供其他程序调用的方式,并实现低时延(<0.5s)直播推理_yolov5 dataset = loadstreams-程序员宅基地

文章浏览阅读1.5w次,点赞50次,收藏322次。将yolov5的推理代码改成可供其它程序调用的方式,并实现低时延(<0.5s)直播推理yolov5的代码具有高度的模块化,对于初学者十分友好,但是如果咱们要做二次开发,想直接调用其中一些函数,恐怕还是得费一番功夫。参考https://www.pythonheidong.com/blog/article/851830/44a42d351037d307d02d/和https://blog.csdn.net/ld_long/article/details/113920521(不知道为什么失效了)实现_yolov5 dataset = loadstreams

JavaScript之Proxy详解_js中proxy-程序员宅基地

文章浏览阅读445次,点赞8次,收藏10次。是JavaScript中的一个强大而灵活的特性,它允许你创建一个代理对象,可以拦截并改变对象的底层操作。是ES6引入的一个新对象,用于创建一个对象的代理,可以拦截并重定义基本的操作。提供了丰富的拦截操作,使得我们能够对对象的行为进行灵活的定制。可以实现对象属性的懒加载,只在访问时才进行实际的计算或获取。能够实现更清晰和易读的代码,避免了传统的一些hack手段。的特性和应用场景,有助于更好地利用它提供的强大功能。是ES6引入的特性,不支持ES6的环境无法使用。可以实现数据绑定,监听对象属性的变化。_js中proxy

手机上用Exchange 协议配置收发QQ邮箱_公司exchange协议邮件只能用手机qq邮箱登录-程序员宅基地

文章浏览阅读2k次。手机上用Exchange 协议配置收发QQ邮箱_公司exchange协议邮件只能用手机qq邮箱登录

【永久免费】9000w+海外动态IP_英国动态住宅ip有免费分享的吗-程序员宅基地

文章浏览阅读437次,点赞4次,收藏7次。官网:https://my.socks5.io#SCPJLCFHWR。全球200+国家,9000w+动态IP,没任何费用!动态住宅IP/静态住宅IP/机房IP等,永久免费!【全球代理IP免费用】!点击注册,永久免费海外IP._英国动态住宅ip有免费分享的吗

SSL/TSL双向认证过程与Wireshark抓包分析-程序员宅基地

文章浏览阅读1.6k次。2018年07月12日 19:19:41Joohong阅读数 1831版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingzi123456789/article/details/810207471、 SSL/TSL基本知识(1)SSL/TLS协议运行机制:https://blog.csdn.net/fw0124/arti..._ssl/tsl双向认证过程与wireshark抓包分析

修改yum的更新源vi /etc/yum.repos.d/CentOS-Base.repo-程序员宅基地

文章浏览阅读3.6k次。2019独角兽企业重金招聘Python工程师标准>>> ..._修改/etc/yum.repos.d/centos-base.repo文件,将原有的镜像地址替换为新的、仍然提