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

智能推荐

【ESP8266之LUA开发】九、File操作,实现C#改变并存储工作模式,SSID与PWD的保存与读取_esp8266 configfile.close()-程序员宅基地

文章浏览阅读1.5k次。emmmm,这一片信息量很大,算是一个综合的过程吧,如果哪里有疑问记得及时查看前面的内容,查漏补缺。文件操作,保存数据,这样的话就可以随意修改启动时工作在哪一种模式,哪一种通信,以及其余需要保存在模块内部的信息。 就实现上位机软件的第一个功能,,修改启动模式 对应的C#代码private void button2_Click(object sender, EventArgs e)_esp8266 configfile.close()

【Unity】如何优雅地移动物体-8个方法_unity 物体移动-程序员宅基地

文章浏览阅读8.1k次,点赞58次,收藏136次。在游戏开发中,如何移动物体?是我们需要思考的事情。Unity 引擎也提供了众多的方法,每个开发者的使用习惯也各不相同,所以往往不是很清楚在这种场景下哪种方式最好的或者最有效的。那么,这篇文章,我想分享一下移动物体的一些方法和优缺点。_unity 物体移动

【Python】python 反射机制在实际的应用场景讲解-程序员宅基地

文章浏览阅读97次。 剖析python语言中 "反射" 机制的本质和实际应用场景一. 前言def s1(): print("s1是这个函数的名字!") s = "s1"print("%s是个字符串" % s)在上面的代码中,我们必须区分两个概念,f1和“f1"。前者是函数f1的函数名,后者只是一个叫”f1“的字符串,两者是不同的事物。我们可以用f1()的方式调用函数f1,但我们不能用"f..._python反射机制及应用场景

uni-app上传文件(带进度条)_uniapp 上传视频时显示进度-程序员宅基地

文章浏览阅读3.6k次。<template> <view> <view> <progress :percent="percent" stroke-width="10"></progress> </view> <view> <button type="primary" :loading="loading" :disabled="disabled" @click="upload">选择照片</button&g_uniapp 上传视频时显示进度

pthread_mutex_lock引起的core_thread mutex lock segmentation fault-程序员宅基地

文章浏览阅读5.9k次。遇到一个奇怪的corecore在pthread_mutex_lock下一行最后发现:_thread mutex lock segmentation fault

SuperSocket 一个轻量级可扩展 Socket 开发框架_super socket-程序员宅基地

文章浏览阅读461次,点赞7次,收藏8次。不用多说这是迄今为止用过最好用的.Net领域Socket开发框架,从Photon 脱坑之后,一直用它来搭建游戏服务器,相伴多年经历了多个游戏和工控项目的检验,简单易上手,功能应有尽有,强烈推荐。可以通过NuGet进行安装。_super socket

随便推点

公众号模板消息推送_公众号平台模板推送流程-程序员宅基地

文章浏览阅读365次。首先公众号和小程序需要在同一主体下需要在公众号“广告与服务”→“模板消息”中申请自己所需的模板需要在公众号“设置与开发”→“基本配置”拿到appid,appsecret及配置服务器对应的IP白名单其次推送需要推送人员关注该公众号且拿到该用户的openid进行推送最后点击模板消息进入小程序的话,还需要提供小程序的appid和path信息以上就是公众号推送的大致流程啦_公众号平台模板推送流程

python编程从入门到实践第五章习题_python快速编程入门第五章课后题-程序员宅基地

文章浏览阅读1.2k次。第五章讲的是if的一些用法,和C、C++相比,只是把当中的else if 换成了elif,其他逻辑等完全没有任何变化。总的来说还是没有任何难度的,只是每一个语句后面需要加一个“:”,这个是初学的时候特别容易遗漏的。好了,直接上代码:#5-1car = 'subaru'print("Is car == 'subaru'? I Predict True.")print(car=='subaru')..._python快速编程入门第五章课后题

基于SpringBoot的社区医院管理服务系统的设计与实现_基于springboot的社区医疗服务管理系统-程序员宅基地

文章浏览阅读1.3k次。因此,系统无疑给人们的生活带来了极大的方便,网络的应用让时间和距离不再是局限。通过前面的功能分析可以将社区医院管理服务系统的功能分为管理员、用户和医生三个部分,系统的主要功能包括首页、个人中心、用户管理、医生管理、预约医生管理、就诊信息管理、诊疗方案管理、病历信息管理、健康档案管理、费用信息管理、系统管理等内容。预约医生管理,在预约医生管理页面中可以对索引、预约编号、医生账号、医生姓名、预约时间、科室、用户账号、用户姓名、审核回复、审核状态、审核等内容进行详情、就诊、修改或删除等操作,如图5-21所示。_基于springboot的社区医疗服务管理系统

374. Guess Number Higher or Lower(猜数字大小)-程序员宅基地

文章浏览阅读304次,点赞10次,收藏9次。返回我选出的数字。

Eigen中三维位姿表示方式以及相互转换_eigen::matrix3d-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏11次。Eigen中三维位姿表示方式以及相互转换_eigen::matrix3d

python Tk()、Frame()、TopLevel()用法-程序员宅基地

文章浏览阅读1w次,点赞9次,收藏24次。Tk():创建应用程序主窗口Frame():创建控件容器,可依附在窗口中TopLevel():创建弹出式窗口示例1:import tkinter#主窗口:window = tkinter.Tk()#创建窗口window.title("简易版小程序")#设置标题#创建控件容器frameDemo1frameDemo1 = tkinter.Frame(window)#向fram..._tk()

推荐文章

热门文章

相关标签