Algoogle

Algorithm for Programming Contest

Typical DP Contest B - ゲーム

Category: TDPC Tag: dp

B - ゲーム

問題概要


解法

#code1
メモをペアにして, ペアを交互に見ることで順番に最善手を尽くすようにした.

#code2
dp[i][j] := 左の山が i 個, 右の山が j このこっている状態からスタートするときの先手の取るものの価値の和


コード


#code2

(B.d) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.bigint, std.math, std.range;
import core.thread;

//Input
string[] tokens;
int tokenId = 0;
string readToken(){
    for(;tokenId == tokens.length;)
        tokens = readln.split, tokenId = 0;
    return tokens[tokenId++];
}
int readInt(){ return to!int(readToken);}

//chmin/chmax
void chmin(T)(ref T t, T f) { if (t > f) t = f; }
void chmax(T)(ref T t, T f) { if (t < f) t = f; }

//Pair
struct Pair(S, T) {
    S x; T y;
    int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
    string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

int A, B;
int[1024] a, b;
Pair!(int, int)[1024][1024] memo;

void main(){
    A = readInt;
    B = readInt;
    foreach(i;0..1024)
        foreach(j;0..1024) memo[i][j] = pair(-1,-1);
    foreach(i;0..A) a[i] = readInt;
    foreach(i;0..B) b[i] = readInt;
    writeln(rec(0,0).x);
}

Pair!(int,int) rec(int i, int j){
    if(memo[i][j].x >= 0) return memo[i][j];
    if(i == A && j == B) return (memo[i][j] = pair(0,0));
    if(i == A){
        auto tmp = rec(i, j + 1);
        swap(tmp.x, tmp.y);
        tmp.x += b[j];
        return (memo[i][j] = tmp);
    }
    if(j == B){
        auto tmp = rec(i + 1, j);
        swap(tmp.x, tmp.y);
        tmp.x += a[i];
        return (memo[i][j] = tmp);
    }
    auto ret = rec(i + 1, j), hoge = rec(i, j + 1);
    swap(ret.x, ret.y);
    swap(hoge.x, hoge.y);
    ret.x += a[i];
    hoge.x += b[j];
    chmax(ret, hoge);
    return (memo[i][j] = ret);
}

#code2

(B.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <algorithm>
#include <iostream>
#define int long long
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) repi(i,0,a)
using namespace std;

int A, B;
int a[1024], b[1024];
int suma[1024], sumb[1024];
int dp[1024][1024];

void input(){
    cin >> A >> B;
    rep(i,A) cin >> a[i];
    rep(i,B) cin >> b[i];
    reverse(a,a+A+1); reverse(b,b+B+1);
    repi(i,1,A+1) suma[i] += suma[i-1] + a[i];
    repi(i,1,B+1) sumb[i] += sumb[i-1] + b[i];
}

int solve(){
    rep(i,A+1) rep(j,B+1){
        if(i) dp[i][j] = max(dp[i][j], suma[i-1] + sumb[j] - dp[i-1][j] + a[i]);
        if(j) dp[i][j] = max(dp[i][j], suma[i] + sumb[j-1] - dp[i][j-1] + b[j]);
    }
    return dp[A][B];
}

signed main(){
    input();
    cout << solve() << endl;
    return 0;
}

Comments