51isoft's ACM Journey

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  5 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
Problem Description
The Association of Chess Monsters (ACM) is planning their annual team match up against the rest of the world. The match will be on 30 boards, with 15 players playing white and 15 players playing black. ACM has many players to choose from, and they try to pick the best team they can. The ability of each player for playing white is measured on a scale from 1 to 100 and the same for playing black. During the match a player can play white or black but not both. The value of a team is the total of players' abilities to play white for players designated to play white and players' abilities to play black for players designated to play black. ACM wants to pick up a team with the highest total value.
 

Input
Input consists of a sequence of lines giving players' abilities. Each line gives the abilities of a single player by two integer numbers separated by a single space. The first number is the player's ability to play white and the second is the player's ability to play black. There will be no less than 30 and no more than 1000 lines on input.
 

Output
Output a single line containing an integer number giving the value of the best chess team that ACM can assemble.
 

Sample Input
87 84
66 78
86 94
93 87
72 100
78 63
60 91
77 64
77 91
87 73
69 62
80 68
81 83
74 63
86 68
53 80
59 73
68 70
57 94
93 62
74 80
70 72
88 85
75 99
71 66
77 64
81 92
74 57
71 63
82 97
76 56
 

Sample Output
2506
 

题意大概是在一堆人里选择30个人(一半执黑,另一半执白),使棋力和最大。

算法:DP。f[i][j][k]表示前i个人中选j个执黑k个执白的最大棋力。

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

int f[2000][16][16];
int a[2000][2];
int n;

int i,j,k;

int main()
{

    n
=0;
    
while (scanf("%d %d",&a[n][0],&a[n][1])!=EOF) n++;
    
for (i=1;i<16;i++) f[0][0][i]=a[0][1];
    
for (i=1;i<16;i++) f[0][i][0]=a[0][0];
    
for (i=1;i<n;i++)
    
{
        
for (j=0;j<16;j++)
        
{
            
for (k=0;k<16;k++)
            
{
                
if (j==0) f[i][1][k]=f[i-1][0][k]+a[i][0];
                
if (k==0) f[i][j][1]=f[i-1][j][0]+a[i][1];
                
if (j!=0
                    
if (f[i-1][j-1][k]+a[i][0]>f[i-1][j][k]) 
                    
{
                        
if (f[i][j][k]<f[i-1][j-1][k]+a[i][0]) f[i][j][k]=f[i-1][j-1][k]+a[i][0]; 
                    }

                    
else if (f[i][j][k]<f[i-1][j][k]) f[i][j][k]=f[i-1][j][k];
                
if (k!=0
                    
if (f[i-1][j][k-1]+a[i][1]>f[i-1][j][k]) 
                    
{
                        
if (f[i][j][k]<f[i-1][j][k-1]+a[i][1]) f[i][j][k]=f[i-1][j][k-1]+a[i][1]; 
                    }

                    
else if (f[i][j][k]<f[i-1][j][k]) f[i][j][k]=f[i-1][j][k];
            }

        }

    }

    printf(
"%d\n",f[n-1][15][15]);
    
return 0;
}

posted on 2008-09-18 17:20 51isoft 阅读(909) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理