hdu1052Tian 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): 37436 Accepted Submission(s): 11248
"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.
20 20
20 19
题意:田忌赛马,知道了田忌的n只马的速度和齐王的n只马的速度,赢一场得到200块,输一场失去200块,平局保持不变。田忌想要更多钱。输出田忌最后最多能拿到多少钱。
题解:把田忌和王的马按速度从大到小排序。之后就是超多的细节。。。。。。。。。具体看代码注释
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,t1,t2,k1,k2;
while(~scanf("%d",&n),n)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<n;i++)
{
scanf("%d",&b[i]);
}
sort(a,a+n,cmp);//按速度从大到小排序
sort(b,b+n,cmp);
t1=k1=;//田忌和齐王最强的马的下标
t2=k2=n-;//田忌和齐王最差的马的下标
int ans=;
while(t1<=t2)
{
if(a[t1]>b[k1])//田忌的最强比王的最强更强,
{
ans+=;t1++;k1++;
}
else if(a[t1]<b[k1])//田忌的最强比王的最强弱,用最弱的去比
{
ans-=;t2--;k1++;
}
else//最强的马速度一样
{
if(a[t2]>b[k2])//田忌最慢的马比王最慢的马快
{
ans+=;t2--;k2--;
}
else if(a[t2]==b[k1])//田忌最慢的马和王最快的马速度一样,平局
{
k1++;t2--;
}
else//田忌最慢的马和齐王最快的马比
{
ans-=;k1++;t2--;
}
}
}
printf("%d\n",ans);
}
return ;
}
最新文章
- 2000条你应知的WPF小姿势 基础篇<;1-7>;
- Windows Phone 8.1 新特性 - 控件之FlipView
- IOS中货币高精度要求使用NSDecialNumber、
- 项目中常用SQL语句总结
- MongoDB基本命令随便敲敲
- bokeh-scala
- 在mac上访问自带服务器权限问题
- USACO Longest Prefix 【水】
- 30第二建筑Github Page
- Class Model of Quick Time Plugin
- java代码之美(2)---Java8 Stream
- Java开发笔记(十四)几种运算符的优先级顺序
- flex外包团队—北京动点软件:推荐一本不错的Flex书籍
- Webstorm使用教程详解
- .NET 获取类内成员的名称,以字符串形式显示
- 10分钟让你明白MySQL是如何利用索引的
- 转:基于InstallShield2013LimitedEdition的安装包制作
- 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)
- 【C++】int与string互转
- 72.Edit Distance---dp