(简单) dp HOJ 2091 Chess

来源:互联网 发布:碧柔防晒知乎 编辑:程序博客网 时间:2024/06/05 20:46

Chess

My Tags  (Edit)
Source : Univ. of Alberta Local Contest 1999.10.16Time limit : 1 secMemory limit : 32 M

Submitted : 237, Accepted : 94

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.

There are multiple test cases. Each case will be followed by a single line containing a "*".

Output

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

Sample Input

87 8466 7886 9493 8772 10078 6360 9177 6477 9187 7369 6280 6881 8374 6386 6853 8059 7368 7057 9493 6274 8070 7288 8575 9971 6677 6481 9274 5771 6382 9776 56*

Sample Output

2506

题意:给出很多选手,他们能去黑组和白组中的其中一个,我们要凑成一个15人黑组,15人白组,而且要分数最高。
思路:很明显的一个动态规划吧 , 我们用dp[i][j][k] 表示前i个人,凑成黑组j个人,白组k个人的最高得分,那么状态转移方程为dp[i][j][k] = max(dp[i-1][j-1][k]+black[i],dp[i-1][j][k-1]+white[i]);   然后这里方程只跟i-1有关,所以dp可以减少一维,变成dp[j][k] = max(dp[j-1][k]+black[i],dp[j][k-1]+white[i],dp[j][k]); 然后j和k要倒着退,这个思想在背包里面有用到过。然后答案就是dp[15][15];

代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<map>
#include<cstdio>
#include<string.h>
using namespace std;

const int inf = 0xfffffff;
int dp[20][20];
int white[1010];
int black[1010];
int n;

void solve()
{
    for (int i = 0 ; i <= 15 ; ++i)
        for (int j = 0 ; j <= 15 ; ++j)
            dp[i][j] = -inf;
    dp[0][0] = 0;
    for (int i = 1 ; i <= n ; ++i)
    {
        for (int j = 15 ; j >= 0 ; --j)
        {
            for (int k = 15 ; k >= 0 ; --k)
            {
                if (k>=1 && dp[j][k-1]!=-inf)
                    dp[j][k] = max(dp[j][k],dp[j][k-1]+black[i]);
                if (j>=1 && dp[j-1][k]!=-inf)
                    dp[j][k] = max(dp[j][k],dp[j-1][k]+white[i]);
            }
        }
    }
    printf("%d\n",dp[15][15]);
}

int main()
{
    char buffer[110];
    n = 0;
    while (gets(buffer))
    {
        if (buffer[0]=='*')
        {
            solve();
            n = 0;
            continue;
        }
        ++n;
        sscanf(buffer,"%d%d",white+n,black+n);
    }
}
0 0
原创粉丝点击