Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

uva 1344 - Tian Ji -- The Horse Racing(贪心)

$
0
0

题目链接:uva 1344 - Tian Ji -- The Horse Racing


题目大意:田季要和齐王赛马,两人各有n只马,然后分别给出田季和齐王的马的速度,赢一场获得200两,输一场失去200两,平局无损失。问田季至多能赢多少。


解题思路:贪心,但是用了一个很挫的方法去判断速度相等的情况。


#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int N = 1005;

int n, ans, t[N], q[N];

bool cmp(const int& a, const int& b) {
	return a > b;
}

void judge(int w, int e) {
	int sum = (2 * w + e - n) * 200;
	if (sum > ans) ans = sum;
}

void solve(int x, int y, int w, int e) {

	if (y >= n || x >= n) {
		judge(w, e);
		return ;
	}

	while(t[x] < q[y]) {
		y++;
		if (y >= n) {
			judge(w, e);
			return;
		}
	}

	if (t[x] == q[y]) {
		solve(x + 1, y + 1, w, e + 1);
		solve(x, y + 1, w, e);
	} else {
		solve(x + 1, y + 1, w + 1, e);
	}
}

int main () {
	while (scanf("%d", &n) == 1 && n) {
		ans = -200 * n;
		for (int i = 0; i < n; i++) scanf("%d", &t[i]);
		for (int i = 0; i < n; i++) scanf("%d", &q[i]);
		sort(t, t + n, cmp); sort(q, q + n, cmp);

		solve(0, 0, 0, 0);
		printf("%d\n", ans);
	}
	return 0;
}


作者:u011328934 发表于2013-12-20 0:33:16 原文链接
阅读:103 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>