Topic Links:
http://poj.org/problem?id=2287
Main topic:
Tian Bogey and King Horse racing, two people each have n horse, each horse has a speed, run fast wins, slow lose. Tian Bogey every win one
200, flat, no money, lost. 200. Every time the king first, Tian bogey again. Q: What is the maximum amount of tin bogey?
Ideas:
Greedy thought. Now the field bogey and the King's horse to sort. Tian bogey horse speed from small to large arrangement, King's horse speed from big to small row
Column. In order to win as much as possible, Tian bogey will take the following strategies:
1) As far as possible to use their own low horse to win the King's Fast horse.
2) The rest of the winning horse, as far as possible with his horse and the King's Horse to tie
3) The rest of the horse can not win the game, but also can not be a tie to lose
Use numa[] array and numb[] array to mark the horses that have been known to win and lose, traverse two times the field bogey and the King's horse, find out for the first time
To win the King's horse, the second time from the rest of the horse to find the maping hand with the King's Horse. Then calculate the earned money.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int a[1100],b[1100],numa[1100],numb[1100];int cmp (int a,int b) {return A > B;} int main () {int N; while (~SCANF ("%d", &n)) {for (int i = 0; i < N; ++i) scanf ("%d", &a[i]); for (int i = 0; i < N; ++i) scanf ("%d", &b[i]); Sort (a,a+n); Sort (b,b+n,cmp); memset (numa,0,sizeof (NumA)); memset (numb,0,sizeof (NumB)); Try to win for (int i = 0; i < N; ++i)//Field bogey {for (int j = 0; j < N; ++j)//King { if (A[i] > b[j] &&! Numa[i] &&! Numb[j]) {numa[i] = 1; NUMB[J] = 1; Break }}}//Win as much as possible for (int i = 0; i < N; ++i) {for (int j = 0; J < N ++J) {if (a[i] = = B[j] &&! Numa[i] &&! Numb[j]) {numa[i] = 2; NUMB[J] = 2; Break }}} int ans = 0; for (int i = 0; i < N; ++i) if (numa[i] = = 1) ans + = 200; else if (numa[i] = = 2) continue; else ans-= 200; printf ("%d\n", ans); } return 0;}
POJ2287 Tian Ji--The Horse Racing "greed"