[ ZJOI 2009 ] 取石子游戏

题目

Luogu
Acwing

思路

1.png
2.png
3.png
4.png
5.png

代码

#include <iostream>
using namespace std;
const int N = 1010;
int T, n, a[N], l[N][N], r[N][N];
int main(){
    cin >> T;
    while (T-- && cin >> n) {
        for (int i = 1; i <= n; i ++ ) cin >> a[i];
        for (int len = 1; len <= n; len ++ )
            for (int i = 1; i + len - 1 <= n; i ++ ) {
                int j = i + len - 1;
                if (len == 1) l[i][j] = r[i][j] = a[i];
                else {
                    int L = l[i][j - 1], R = r[i][j - 1], X = a[j];
                    if (R == X) l[i][j] = 0;
                    else if (X < L && X < R || X > L && X > R) l[i][j] = X;
                    else if (L > R) l[i][j] = X - 1;
                    else l[i][j] = X + 1;
                    L = l[i + 1][j], R = r[i + 1][j], X = a[i];
                    if (L == X) r[i][j] = 0;
                    else if (X < L && X < R || X > L && X > R) r[i][j] = X;
                    else if (R > L) r[i][j] = X - 1;
                    else r[i][j] = X + 1;
                }
            }
        if (n == 1) cout << 1 << endl;
        else cout << (l[2][n] != a[1]) << endl;
    }
    return 0;
}
posted @ 2021-04-02 16:14  Protein_lzl  阅读(75)  评论(0编辑  收藏  举报