我的第一篇blog—— 一起来赛马呀
Posted seek you
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的第一篇blog—— 一起来赛马呀相关的知识,希望对你有一定的参考价值。
作为一名大三的学生现在开始学习acm,或许太晚。感叹时光蹉跎。。。。我的blog将以讲解的形式的发布,以专题的形式的形式介绍一些基本的知识和经典的题目。虽然感觉自己所剩时间无多,但也希望起到前人种树的作用,让我们一起加油! 接下来让我们以一道古老而又有趣的题目点燃我们对acm的热情吧。(此处向孙膑献上orz)
xdoj1240:http://acm.xidian.edu.cn/problem.php?id=1240
1240: Godv与女朋友赛马
时间限制: 2 Sec 内存限制: 128 MB提交: 13 解决: 8
[提交][状态][讨论版]
题目描述
到七夕啦!在这样的日子里,Godv当然要去陪女朋友玩耍啦~~~(不要问我敬老师的手环送没送出去!)于是,资产阶级的Godv选择了带女朋友去玩赛马~~~
话说,两个人各有n匹马,每匹马各有各的速度,两个人一共比n场,且每匹马恰参加一次比赛。两个人事先约定好,对于每场比赛,获胜的得一分,平局或失败不得分,最后输掉比赛的要请对方晚上看电影。
Godv这样既聪明又萌萌哒又体贴女朋友的人,当然是不想让女朋友输啦。于是他事先要对比赛做了手脚,这样每一场比赛的参赛的马儿都是被Godv钦定好了的。
下面,你能帮Godv算一算,Godv最多能让女朋友赢多少分吗?
输入
多组数据,数据组数不超过10组,请处理到文件结束。每组数据第一行包含一个数n(1<=n<=1e5),第二行包含n个正整数 ai (1<=ai<=1e9),表示Godv的第i匹马的速度,第三行包含n个正整数 bi (1<=bi<=1e9),表示Godv的女朋友的第i匹马的速度。
输出
对于每组数据都输出一行。如果Godv的女朋友只能得到0分,请输出”Godv too strong”,否则,输出Godv的女朋友最多能得到的分数。
样例输入
3 1 2 3 1 2 3 1 2 1
样例输出
2 Godv too strong
题目分析:两个人各n匹马相互比赛,赢了得一分,输了平了不扣分。这里我们采取贪心策略,先将马的速度进行排序,若女朋友最快的马大于glory最快的马,则与它相配。因为女朋友最快的的马反正都要得分,拿我就消耗对方最大的,否则不是亏了吗。如女朋友的马小于glory最快的马,相当于女朋友所有的马跟它比都赢不了,那我就用我最慢的马跟你比。 这样就能保证得到最大分了。献上代码。
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int N=1e5+7; 5 long long x[N]; 6 long long y[N]; 7 int n; 8 bool mycmp (int a,int b) { 9 return (a>b); 10 } 11 int main () 12 { 13 while (scanf("%d",&n)!=EOF) { 14 for (int i=1;i<=n;i++) 15 scanf("%lld",&x[i]); 16 for (int i=1;i<=n;i++) 17 scanf("%lld",&y[i]); 18 sort(x+1,x+1+n,mycmp); 19 sort(y+1,y+1+n,mycmp); 20 int l1,l2; 21 int h1,h2; 22 h1=h2=1; 23 l1=l2=n; 24 int ans=0; 25 while (n--) { 26 if (y[l2]>x[l1]) { 27 ans++; 28 l1--; 29 l2--; 30 } 31 else { 32 l2--; 33 h1++; 34 } 35 } 36 if (ans==0) printf("Godv too strong\n"); 37 else printf("%d\n",ans); 38 } 39 return 0; 40 }
这只是小试牛刀,让我们看看hdoj 1052:http://acm.split.hdu.edu.cn/showproblem.php?pid=1052
Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31709 Accepted Submission(s): 9624
"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.
#include<cstdio> #include<algorithm> using namespace std; const int N=1007; int a[N]; int b[N]; int n; int main () { while (~scanf("%d",&n)&&n!=0) { for (int i=1;i<=n;i++) scanf ("%d",&a[i]); for (int i=1;i<=n;i++) scanf ("%d",&b[i]); sort (a+1,a+1+n); sort (b+1,b+1+n); int ans=0; int h1,l1; int h2,l2; h2=h1=n; l2=l1=1; while (n--) { if (a[h2]>b[h1]) { ans++; h2--; h1--; } else if(a[l2]>b[l1]) { ans++; l2++; l1++; } else { if (a[l2]<b[h1]) ans--; l2++; h1--; } } printf("%d\n",ans*200); } return 0; }
小时候以为田忌赛马是一件太过简单的东西,此处向孙膑再次献上我的orz...
下期专题递归,欢迎留言哦!!!!!!!!!!!!!!!
以上是关于我的第一篇blog—— 一起来赛马呀的主要内容,如果未能解决你的问题,请参考以下文章