POJ-2287 Tian Ji -- The Horse Racing 贪心 | DP

  题目链接:http://poj.org/problem?id=2287

  题目很容易误以为是最大匹配,不过O( n^3 )的复杂度承受不了,必须用贪心或者DP策略了。

  容易想到先排序,然后贪心,贪心策略如下:

    1.如果田忌最慢的马比齐王最慢的马快,那么赢一局。

    2.如果田忌最慢的马比齐王最慢的马慢,那么拿田忌最慢的马浪费掉齐王最快的马,输一局。

    3.如果田忌最慢的马和齐王最慢的马一样快:

      (1)如果田忌最快的马比齐王最快的马快,那么赢一局。

      (2)如果田忌最快的马比齐王最慢的马慢,那么拿田忌最慢的马浪费掉齐王最快的马,输一局。

      (3)如果田忌最快的马和齐王最快的马一样快,那么拿田忌最慢的马和齐王最快的马比。

  总的来说,贪心策略就是,有赢得情况就赢,否则就输。

  DP和贪心差不多,因为是稀疏DP:

    dp[i][j]=MAX(dp[i-1][j-1]+g[i][j],dp[i-1][j]+g[i][n-i+j+1]);
    d[i][j]:用dp[i,j]表示齐王出了i匹较强的马和田忌的j匹较强的马,i-j匹较弱的马比赛之后,田忌所能够得到的最大盈利。
    g[i][j]:第i强的齐王马和第j强的田忌马比的盈利(或亏或平或胜利)。

贪心代码:

 1 //STATUS:C++_AC_47MS_180KB

 2 #include <functional>

 3 #include <algorithm>

 4 #include <iostream>

 5 //#include <ext/rope>

 6 #include <fstream>

 7 #include <sstream>

 8 #include <iomanip>

 9 #include <numeric>

10 #include <cstring>

11 #include <cassert>

12 #include <cstdio>

13 #include <string>

14 #include <vector>

15 #include <bitset>

16 #include <queue>

17 #include <stack>

18 #include <cmath>

19 #include <ctime>

20 #include <list>

21 #include <set>

22 #include <map>

23 using namespace std;

24 //define

25 #define pii pair<int,int>

26 #define mem(a,b) memset(a,b,sizeof(a))

27 #define lson l,mid,rt<<1

28 #define rson mid+1,r,rt<<1|1

29 #define PI acos(-1.0)

30 //typedef

31 typedef __int64 LL;

32 typedef unsigned __int64 ULL;

33 //const

34 const int N=1010;

35 const int INF=0x3f3f3f3f;

36 const int MOD=256,STA=8000010;

37 const LL LNF=1LL<<60;

38 const double EPS=1e-8;

39 const double OO=1e15;

40 const int dx[4]={-1,0,1,0};

41 const int dy[4]={0,1,0,-1};

42 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

43 //Daily Use ...

44 inline int sign(double x){return (x>EPS)-(x<-EPS);}

45 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

46 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

47 template<class T> inline T Min(T a,T b){return a<b?a:b;}

48 template<class T> inline T Max(T a,T b){return a>b?a:b;}

49 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

50 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

51 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

52 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

53 //End

54 

55 int x[N],y[N];

56 int n;

57 

58 int main()

59 {

60  //   freopen("in.txt","r",stdin);

61     int i,j,ans,lx,rx,ly,ry;

62     while(~scanf("%d",&n) && n)

63     {

64         for(i=0;i<n;i++)

65             scanf("%d",&x[i]);

66         for(i=0;i<n;i++)

67             scanf("%d",&y[i]);

68 

69         sort(x,x+n);

70         sort(y,y+n);

71         lx=ly=ans=0;rx=ry=n-1;

72         while(lx<=rx){

73             if(x[lx]>y[ly]){

74                 lx++,ly++;

75                 ans+=200;

76             }

77             else if(x[lx]<y[ly]){

78                 lx++,ry--;

79                 ans-=200;

80             }

81             else {

82                 if(x[rx]>y[ry]){

83                     rx--,ry--;

84                     ans+=200;

85                 }

86                 else {

87                     if(x[lx]<y[ry])ans-=200;

88                     lx++,ry--;

89                 }

90             }

91         }

92 

93         printf("%d\n",ans);

94     }

95     return 0;

96 }

 

你可能感兴趣的:(poj)