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-655-光棍的yy(大斐波那契数列)
题目链接 /* 思路: 考察大斐波那契数列 */ import java.util.*; import java.math.*; public class Main{ public static vo ...
- hdu-1102-Constructing Roads(Prim算法模板)
题目链接 /* Name:hdu-1102-Constructing Roads Copyright: Author: Date: 2018/4/18 9:35:08 Description: pr ...
- C8051F340 USB Fn hacking
/************************************************************************************ * C8051F340 US ...
- 【java规则引擎】之规则引擎解释
转载:http://www.open-open.com/lib/view/open1417528754230.html 现实生活中,规则无处不在.法律.法规和各种制度均是:对于企业级应用来说,在IT技 ...
- STL容器共性机制和使用场景
一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...
- mysql之 double write 浅析
http://blog.itpub.net/22664653/viewspace-1140915/ 介绍double write之前我们有必要了解partial page write 问题 : ...
- loj 6485 LJJ学二项式定理 —— 单位根反演
题目:https://loj.ac/problem/6485 先把 \( a_{i mod 4} \) 处理掉,其实就是 \( \sum\limits_{i=0}^{3} a_{i} \sum\lim ...
- TreeView 树节点的处理
TreeView 树节点的处理 using System; using System.Collections.Generic; using System.ComponentModel; using S ...
- Day2-VIM(一):移动
基础 字符移动 k 上移 k h 左移 h l l 右移 j j 下移 你也可以使用键盘上的方向键来移动,但这么做h j k l的存在就失去了意义 之所以使用h j k l来控制方向,其主要目的是让你 ...
- L3-001. 凑零钱(dfs或者01背包)
L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现 ...