poj2287 Tian Ji -- The Horse Racing
分析
这个题和传统的田忌赛马不一样的地方就是多了平局情况,所有我们不难想到要用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的更多相关文章
- POJ-2287.Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17662 Acc ...
- 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 ...
- Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Jav ...
- 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 ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- hdu1052 Tian Ji -- The Horse Racing 馋
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...
- 杭州电 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 ...
- 【贪心】[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 ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
随机推荐
- nyoj-1016-德莱联盟(向量叉乘判断线段相交)
叉乘的坐标表示: A(X1,Y1), B(X2, Y2), C(XC,YC), D(XD, YD);AB = (X2-X1, Y2-Y1);CD = (XD-XC, YD-YC); 向量AB,CD的叉 ...
- 20165210 学习基础和C语言基础调查
20165210 学习基础和C语言基础调查 一.技能学习过程和心得 读了娄老师<做中学>自己还是深有感受的,对于运动.音乐.棋牌都会一点,我觉得做中学可以概括为三点:做,学,学做结合,所谓 ...
- 在VS2013上配置OpenCV1.0
OpenCV1.0版(下载)基本上已经是老掉牙了,但是不想装新版,只是因为任性.所以就有了这样一个新老搭配.装完回想起来还是挺简单的,但是还是费了我一晚上,所以有必要记录一下. 先在电 ...
- awk---Linux下文本处理五大神器之二
转自:http://www.cnblogs.com/dong008259/archive/2011/12/06/2277287.html awk是一个非常棒的数字处理工具.相比于sed常常作用于一整行 ...
- markdown的学习
开始 语法 编辑器 sublime配置 图床 体验 开始 昨天晚上加上今天上午,折腾了算是一天的markdown编辑器. 原因是,为了写博客.在博客园写的东西,想法不到简书里,结果发现有部分乱码,以及 ...
- JDBC小常识
1 JDBC连接数据库6步 Load the JDBC Driver Establish the Database Connection Create a Statement Object Execu ...
- django1.7+nginx1.4.4的static配置
静态文件指像css,js,images之类的文件. 1.工程配置setting.py STATIC_URL = /static/ STATIC_ROOT = /home/www/winingcpi/s ...
- 【转】IMSI和IMEI
国际移动客户识别码(International Mobile Subscriber Identification Number) 为了在无线路径和整个GSM移动通信网上正确地识别某个移动客户,就必须给 ...
- expected declaration specifiers or '...' before string constant
/work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...
- Cordova 3.x 配置文件config.xml强制横屏
官方文档: http://cordova.apache.org/docs/en/3.5.0/config_ref_index.md.html#The%20config.xml%20File 添加 &l ...