传送门

分析

这个题和传统的田忌赛马不一样的地方就是多了平局情况,所有我们不难想到要用dp。我们先将两人的马均降序排列,用dpij表示考虑前i匹马,田忌有几匹马是按从大到小的顺序从头取的(剩下的是从尾部取的)。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
int dp[][],a[],b[];
inline bool cmp(int x,int y){
return x>y;
}
inline int c(int x,int y){
if(x>y)return ;
else if(x<y)return -;
return ;
}
int main(){
int n,i,j;
scanf("%d",&n);
while(n!=){
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)scanf("%d",&b[i]);
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp);
for(i=;i<=n;i++){
for(j=;j<i;j++)
dp[i][j]=max(dp[i-][j-]+c(a[j],b[i]),dp[i-][j]+c(a[n-(i-j-)],b[i]));
dp[i][]=dp[i-][]+c(a[n-i+],b[i]);
dp[i][i]=dp[i-][i-]+c(a[i],b[i]);
}
int ans=-;
for(i=;i<=n;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
scanf("%d",&n);
}
return ;
}

poj2287 Tian Ji -- The Horse Racing的更多相关文章

  1. POJ-2287.Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17662   Acc ...

  2. Hdu 1052 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Jav ...

  4. UVA 1344 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Here is a famous story in Chinese history. That was about 2300 years ago ...

  5. hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

    思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...

  6. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...

  7. 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)

    http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...

  8. 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

  9. 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...

随机推荐

  1. Struts2(2)

    一.分模块开发:单独写模块的配置文件,把配置文件引入到核心配置文件中 Aaction的单独配置文件如下 <?xml version="1.0" encoding=" ...

  2. uva10892(暴力枚举)

    把n的所有因子求出来,总数不会太多,所以直接O(n2)的暴力枚举所有对行不行. 有几个细节要注意,详见代码. #include<iostream> #include<cstdio&g ...

  3. InnoDB参数详解

    1.查询5.5版本的InnoDB参数并注释:[root@localhost etc]# grep -i innodb my.cnf; t_innodb; otherwise, slaves may d ...

  4. PHP提供的数组比较函数总结

    在我们看PHP手册的时候发现,PHP提供了许多数组元素比较的函数,看起来又多又烦又不好记,现在我们来总结一下: sort() — 本函数对数组进行排序,当本函数结束时数组单元将被从最低到最高重新安排. ...

  5. Linux命令学习(18):route命令

    版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的route命令. ...

  6. awk---Linux下文本处理五大神器之二

    转自:http://www.cnblogs.com/dong008259/archive/2011/12/06/2277287.html awk是一个非常棒的数字处理工具.相比于sed常常作用于一整行 ...

  7. LeetCode Count Binary Substrings

    原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...

  8. LeetCode Construct the Rectangle

    原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...

  9. AMD 规范

    AMD(异步模块定义)是为浏览器环境设计的,因为 CommonJS 模块系统是同步加载的,当前浏览器环境还没有准备好同步加载模块的条件. AMD 定义了一套 JavaScript 模块依赖异步加载标准 ...

  10. Windbg内核调试之三: 调试驱动

    这次我们通过一个实际调试驱动的例子,来逐步体会Windbg在内核调试中的作用.由于条件所限,大多数情况下,很多人都是用VMware+Windbg调试内核(VMware的确是个好东西).但这样的调试需要 ...