TOJ 1188.Tian Ji -- The Horse Racing
来源:互联网 发布:苹果恢复大师注册码mac 编辑:程序博客网 时间:2024/05/21 17:22
题目链接
中国古代很有名的田忌赛马问题,题目大意就是这个,原文中出现了一个叫bipartite graph(二分图)的图要注意以下。然后说一下解题思路:分析题目可知,使用贪心算法就可以达到全局最优。下面是贪心解法的代码,值得注意的是第39行的判断
if(tian[lt]<king[hk])
是很有必要的,因为会存在一种特殊情况那就是两方的马都打成平手,此时不需要ans-=200。
#include <stdio.h>#include <algorithm> using namespace std;int main(){ int count; int tian[1001],king[1000]; while(scanf("%d",&count)&&count) { for(int i=0;i<count;i++) scanf("%d",&tian[i]); for(int i=0;i<count;i++) scanf("%d",&king[i]); sort(tian,tian+count); sort(king,king+count); int lk=0,lt=0,hk=count-1,ht=count-1; int ans=0; while(ht>=lt) { if(tian[lt]>king[lk]) { ans+=200; lt++; lk++; } else if(tian[lt]<king[lk]) { ans-=200; lt++; hk--; } else if(tian[lt] == king[lk] && tian[ht]>king[hk]) { ans+=200; ht--; hk--; } else if(tian[lt] == king[lk] && tian[ht]<=king[hk]) { if(tian[lt]<king[hk]) ans-=200; lt++; hk--; } } printf("%d\n",ans); } return 0;}
0 0
- TOJ 1188.Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing (TOJ 1188)
- Toj 1188 Tian Ji -The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing
- IO流面试题
- 集训第七天
- JQueryMobile学习
- Linux中fork函数详解
- Maven学习实战
- TOJ 1188.Tian Ji -- The Horse Racing
- MacBook 下如何配置mysql数据库的编码为utf8
- 异常面试题
- 使用java RMI的步骤
- Apache DbUtils使用
- 堆排序
- 多线程面试题
- Xutils3 数据库模块的使用方法
- 重启Tomcat后还是登录状态