POJ2626Chess题解动态规划DP

Chess
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1985   Accepted: 870

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

Source

The UofA Local 1999.10.16
状态:
d[i][j][k]表示前i个人选j个白k个黑的最大值
状态转移方程:
d[i][j][k]=max{d[i-1][j][k],d[i-1][j-1][k]+a[i],d[i-1][j][k-1]+b[i]}
空间可以降到二维
d[j][k]=max{d[j][k],d[j-1][k]+a[i],d[j][k-1]+b[i]}
代码:
#include<stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) int d[16][16],a,b,j,k; int main() { while(scanf("%d%d",&a,&b)==2) for(j=15;j>=0;j--) for(k=15;k>=0;k--) d[j][k]=max(d[j][k],max(j?d[j-1][k]+a:0,k?d[j][k-1]+b:0)); printf("%d/n",d[15][15]); }

你可能感兴趣的:(POJ2626Chess题解动态规划DP)