田忌赛马

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
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.

 
输入
The input consists of many 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.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
0 题目大意:给田忌n匹马,给国王n匹马,输一轮减少200块钱,赢一轮增加200块钱,平局不奖惩。问最后最多能得到多少钱。 解题思路:如果田忌的慢马能赢国王的,就赢。如果比国王的慢,就拉国王的最快的马比,反正输,不如输的更有价值,为后边的马减小阻力。如果慢马一样快,首先看田忌的最快的马是不是比国王最快的马快,如果是就先让最快的马赢一局。如果不是,就让田忌的最慢的马跟国王的最快的马比较是不是相等,如果不是,那么就输一局;如果是,就让田忌最慢的马跟国王最快的马平局。
#include<bits/stdc++.h>
using namespace std;
int tj[1100],king[1100];
int main(){
int t,i,j,k,tmp,sum,cnt,n,m,tslow,tfast,kslow,kfast;
while(scanf("%d",&n)!=EOF){
memset(tj,0,sizeof(tj));
memset(king,0,sizeof(king));
for(i=0;i<n;i++){
scanf("%d",&tj[i]);
}
for(i=0;i<n;i++){
scanf("%d",&king[i]);
}
sort(tj,tj+n);
sort(king,king+n);
tslow=kslow=0;
tfast=kfast=n-1;
m=k=0;
while(m<n){
if(tj[tslow]>king[kslow]){ //田忌慢马比国王慢马快
tslow++;
kslow++;
k++;
}else if(tj[tslow]<king[kslow]){//田忌慢马比国王慢马慢
tslow++;
kfast--;
k--;
}else{//两人慢马同速
if(tj[tfast]>king[kfast]){//田忌快马比国王快马快
tfast--;
kfast--;
k++;
}else {//田忌快马慢于或等于国王快马
if(tj[tslow]<king[kfast]){//田忌慢马比国王快马慢
kfast--;
tslow++;
k--;
}else if(tj[tslow]==king[kfast]){//田忌慢马等于国王快马
tslow++;
kfast--;
}
}
}
m++;
}
cout<<k*200<<endl;
}
return 0;
}
/*
5
8 6 5 1 3
9 7 6 4 2
*/

  


nyoj 364——田忌赛马——————【贪心】的更多相关文章

  1. nyoj 364 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  2. NYOJ 364 田忌赛马

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Here is a famous story in Chinese history. "That ...

  3. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

  4. hdu1052(田忌赛马 贪心)

    好坑的一道题,不过确实是贪心的一道好题,想了好久一直无法解决平局的情况.  参考了别人的思路,然后结合了自己的想法,总算是想出来了. 这题有些步骤是必须要执行的,有四个步骤 一.当期状态田忌的最慢的马 ...

  5. HDU 1052(田忌赛马 贪心)

    题意是田忌赛马的背景,双方各有n匹马,下面两行分别是田忌和齐王每匹马的速度,要求输出田忌最大的净胜场数*每场的赌金200. 开始的时候想对双方的马匹速度排序,然后比较最快的马,能胜则胜,否则用最慢的马 ...

  6. [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)

    瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...

  7. hdu1052 田忌赛马 —— 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...

  8. poj 1328 Radar Installation(nyoj 287 Radar):贪心

    点击打开链接 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43490   Accep ...

  9. luogu P1650 田忌赛马 |贪心

    题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币. ...

随机推荐

  1. IT学习资源

    介绍个人微信公众平台:Web开发笔记 含有免费学习资源,个人学习笔记,技术文章分享  资源篇 1.webapp书城开发 链接: https://pan.baidu.com/s/1pMHGKrh 密码: ...

  2. 跳转iPhone设置页面,绕过审核

    1.问题描述 跳转iPhone设置页面之前都是通过 App-Prefs:root=WIFI 来跳转,但是2018年6月废弃了这个函数,被认为是私有函数,审核会被拒绝. 有心人采用了字符串转码的方式来规 ...

  3. IP地址和子网划分

    前期知识准备 二进制 和十进制 二进制数据是用0和1表示的数,进位规则为缝二进1, 二进制和十进制的关系 二进  十进 0 1 10 2 100  4 1000 8    10000 16 10000 ...

  4. kali linux之DNS,NTP放大攻击

    DNS放大: 产生大流量的攻击方法-----单机的带宽优势,巨大的单机数量形成的流量汇聚,利用协议特性实现放大效果的流量 DNS协议放大效果----查询请求流量小,但响应流量可能非常巨大(dig AN ...

  5. servlet 核心技术

    servlet 核心技术 servlet 生命周期 在 servlet 生命周期中,servlet 容器完成加载 servlet 类和实例化一个 servlet 实例,并通过3个方法来完成生命周期中的 ...

  6. 开启HTML5之旅。。。

    学习大纲: 一.了解HTML 1.什么是HTML? HTML是用来描述网页的一种语言 HTML是一种超文本标记语言 Hyper Text Markap Language HTML不是一种编程语言,是一 ...

  7. vscode 遇到设置问题

    // 控制是否在新窗口中打开文件. // - default: 除了从软件内部选择的文件 (如,从“文件”菜单选择),其他所有文件都将在新窗口中打开 // - on: 文件将在新窗口中打开 // - ...

  8. 50行代码实现python计算器主要功能

    实现功能:计算带有括号和四则运算的式子   3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4) 基本思路:使用正则表达式提取出每一层 ...

  9. Spring Eureka的使用入门

    Eureka调度服务: Gradle依赖包: 基础框架依赖配置核心代码: buildscript { repositories { mavenCentral() } dependencies {cla ...

  10. 去掉小程序button元素的边框

    button::after {     display:none }