Tian Ji--The Horse Racing Shanghai 2004


动态规划版:


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MAXN(1010);

int arr1[MAXN], arr2[MAXN];
int table[MAXN][MAXN];

inline int scoer(int i, int j)
{
	return arr1[i] > arr2[j]? 1: (arr1[i] < arr2[j]? -1: 0);
}

bool comp(int op1, int op2)
{
	return op1 > op2;
}

int main()
{
	int n;
	while(scanf("%d", &n), n)
	{
		for(int i = 1; i <= n; ++i)
			scanf("%d", arr1+i);
		for(int i = 1; i <= n; ++i)
			scanf("%d", arr2+i);
		sort(arr1+1, arr1+1+n, comp);
		sort(arr2+1, arr2+1+n, comp);
		table[0][0] = 0;
		for(int i = 1; i <= n; ++i)
		{
			table[i][0] = table[i-1][0]+scoer(n-i+1, i);
			for(int j = 1; j < i; ++j)
				table[i][j] = max(table[i-1][j-1]+scoer(j, i), table[i-1][j]+scoer(n-(i-j)+1, i));
			table[i][i] = table[i-1][i-1]+scoer(i, i);
		}
		int ans = table[n][0];
		for(int i = 1; i <= n; ++i)
			ans = max(ans, table[n][i]);
		printf("%d\n", ans*200);
	}
	return 0;
}


贪心版:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MAXN(1010);

int arr1[MAXN], arr2[MAXN];

inline int scoer(int i, int j)
{
	return arr1[i] > arr2[j]? 1: (arr1[i] < arr2[j]? -1: 0);
}

bool comp(int op1, int op2)
{
	return op1 > op2;
}

int main()
{
	int n;
	while(scanf("%d", &n), n)
	{
		for(int i = 1; i <= n; ++i)
			scanf("%d", arr1+i);
		for(int i = 1; i <= n; ++i)
			scanf("%d", arr2+i);
		sort(arr1+1, arr1+1+n, comp);
		sort(arr2+1, arr2+1+n, comp);
		int l1 = 1, r1 = n, l2 = 1, r2 = n;
		int ans = 0;
		while(l1 <= r1)
		{
			if(arr1[l1] > arr2[l2])
			{
				++l1;
				++l2;
				++ans;
			}
			else
				if(arr1[l1] < arr2[l2])
				{
					++l2;
					--r1;
					--ans;
				}
				else
				{
					if(arr1[r1] <= arr2[r2])
					{
						ans += scoer(r1, l2);
						++l2;
						--r1;
					}
					else
					{
						--r1;
						--r2;
						++ans;
					}
				}
		}
		printf("%d\n", ans*200);
	}
	return 0;
}



你可能感兴趣的:(Tian Ji--The Horse Racing Shanghai 2004)