# UVA 1379 Pitcher Rotation

0表示休息。枚举会麻烦一下而已，转移并不难。空间可以用滚动数组优化一下。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

int dp[2][6][6][6][6]; //滚动数组

int k[250];

int n,m,g;
int a[109][109];
vector<int> S[109];

struct node
{
int v;int pos;
}b[109];

bool cmp( node x , node y )
{
return x.v > y.v;
}

void init()
{
cin>>n>>m>>g;
S[0].clear();
S[0].push_back(0);
rep(i,1,m)
{
rep(j,1,n)
{
scanf("%d",&a[i][j]);
b[j].pos = j;
b[j].v = a[i][j];
}
sort(b+1,b+1+n,cmp);
S[i].clear(); // i 表示对手编号为i时，S[i]里面存的投手编号
rep(j,1,5) S[i].push_back( b[j].pos );
}
rep(i,1,g+10) cin>>k[i]; //比赛序列
}

void solve()
{
Clean(dp,-1);
dp[0][0][0][0][0] = 0;
int last = 0 , now = 1;
int ta,tb,tc,td;
rep(i,1,g+10)
{
Clean(dp[now],-1);
ta = i>=2?k[i-1]:0;
tb = i>=3?k[i-2]:0;
tc = i>=4?k[i-3]:0;
td = i>=5?k[i-4]:0;
if ( k[i] != 0 ) //如果当天有比赛
{
for( int x = 0; x < (int)S[ k[i] ].size();x++ ) //枚举当天出赛的人
for( int t1 = 0; t1 < (int)S[ta].size(); t1++ ) //枚举前一天的
{
if ( S[ta][t1] == S[k[i]][x] ) continue;
for( int t2 = 0; t2 < (int)S[tb].size(); t2++ ) //枚举前两天的
{
if ( S[tb][t2] == S[k[i]][x] ) continue;
for( int t3 = 0; t3 < (int)S[tc].size(); t3++ ) //枚举前三天的
{
if ( S[tc][t3] == S[k[i]][x] ) continue;
for( int t4 = 0; t4 < (int)S[td].size(); t4++ ) //枚举前四天的
{
if ( S[td][t4] == S[k[i]][x] ) continue;
if ( dp[last][t4][t3][t2][t1] == -1 ) continue; //上一个状态求过 可以更新当前状态
dp[now][t3][t2][t1][x] = max( dp[now][t3][t2][t1][x] , dp[last][t4][t3][t2][t1] + a[ k[i] ][ S[k[i]][x] ] );
}
}

}
}
}
else
{
for( int t1 = 0; t1 < 5; t1++ )
for( int t2 = 0; t2 < 5; t2++ )
for( int t3 = 0; t3 < 5; t3++ )
for( int t4 = 0; t4 < 5; t4++ )
if ( dp[last][t4][t3][t2][t1] != -1 )
dp[now][t3][t2][t1][0] = max( dp[now][t3][t2][t1][0] ,  dp[last][t4][t3][t2][t1] );
}
now ^= 1;
last ^= 1;
}
int ans = 0;
rep(i,0,4)
rep(j,0,4)
rep(k,0,4)
rep(l,0,4)
ans = max( ans , dp[last][i][j][k][l] );
printf("%0.2f\n",ans/100.0);
}

int main()
{
int T;
cin>>T;
while(T--)
{
init();
solve();
}
return 0;
}

#### uva 1379 - Pitcher Rotation(dp)

2014-05-04 12:54:26

#### UVA 1379 - Pitcher Rotation(DP + 贪心)

2014-05-05 00:25:54

#### UVALive 3679 Pitcher Rotation

2013-09-16 22:00:24

#### Pitcher Rotation - POJ 3392 dp

2015-01-24 15:21:51

#### UVALive 3679 Pitcher Rotation（DP）

2017-03-17 16:36:31

#### 1379号监听员和叶文杰

2016-08-10 18:18:03

#### *(中等)动态规划 HOJ 2508 Pitcher Rotation

2014-02-08 09:53:23

#### bzoj1379 [Baltic2001]Postman

2014-07-10 10:24:17

#### 【FL studio12】神一样的插件-Pitcher

2016-08-16 16:07:53

#### BZOJ 1379 [Baltic 2001] 解题报告

2017-09-22 14:36:13