田忌赛马 - 动态规划

Here is a famous story in Chinese history.

“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.

Sample Input
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample Output
200
0
0

做到一道很神奇的题目,田忌赛马,但是这道题很神奇。
目前半掌握了动态规划的做法,很妙

先对田忌和齐威王的马进行从大到小排序
考虑状态转移方程:f[i][j]表示已经过了i场田忌使用了j匹马的最大收益,有转移方程:
f[i][j]=max(f[i-1][j-1]+get(n-j+1,i),f[i-1][j]+get(i-j,i))
其中get(a,b)是将田a与齐b进行比较而得到的收益值

因此
1.先预处理出按照原顺序得到的收益
2.使用动态规划
f[i-1][j-1]+get(n-j+1,i) 是指田忌选择剩下的马中最后一匹马
f[i-1][j]+get(i-j,i) 是指田忌剩下的马中选择第一匹马
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
 
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define N 1010
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op)  putchar(op); return; }  char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-');  int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op) putchar(op); }
//================================= 
int n;
int a[N],b[N];
int f[N][N];

int get(int x,int y){
	if(a[x]>b[y]) return 1;
	else if(a[x]==b[y]) return 0;
	return -1;
}

//=================================
int main(){
    while(cin >> n,n){
    	for(int i=1;i<=n;i++)
    		cin >> a[i];
    	sort(a+1,a+1+n,greater<int>());
    	for(int j=1;j<=n;j++)
    		cin >> b[j];
    	sort(b+1,b+1+n,greater<int>());
    	
    	memset(f,-0x3a,sizeof f);
    	f[0][0]=0;  //已经经过0场比赛使用0匹马的最大收益
    	for(int i=1;i<=n;i++){
    		for(int j=0;j<=n;j++){
    			if(j==0)
    				f[i][j]=max(f[i][j],f[i-1][j]+get(i-j,i));
    			else 
    				f[i][j]=max(f[i-1][j-1]+get(n-j+1,i),f[i-1][j]+get(i-j,i));
    		}
    	}
    	int maxn=f[n][0];
    	for(int i=1;i<=n;i++)
    		maxn=max(maxn,f[n][i]);
    	cout << maxn*200 << endl;
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值