题解:P16531 [THUPC 2026 决赛] 棋盘对弈游戏
zhang_kevin · · 题解
设
下面先给出一个结论:如果
这是为什么呢?手玩一下可以发现,假设一个人在
因此写个记搜,特判
总时间复杂度为
:::info[参考代码]
提交记录:https://qoj.ac/submission/2967533。
#include<bits/stdc++.h>
#define int long long
#define fo(i, l, r) for(decltype((l) + (r)) i = (l); i <= (r); ++i)
#define fd(i, l, r) for(decltype((l) + (r)) i = (l); i >= (r); --i)
#define fu(i, l, r) for(decltype((l) + (r)) i = (l); i < (r); ++i)
#define y1 zhang_kevin
#define pii pair<int, int>
#define fi first
#define se second
#define vec vector
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ull unsigned long long
#define flush() (fwrite(obuf, 1, p3 - obuf, stdout), p3 = obuf)
using namespace std;
bool ST;
char ibuf[1 << 20], *p1 = ibuf, *p2 = ibuf, obuf[1 << 20], *p3 = obuf;
inline char gc(){
if(p1 == p2){
p1 = ibuf, p2 = ibuf + fread(ibuf, 1, 1 << 20, stdin);
if(p1 == p2) return EOF;
return *p1++;
}
return *p1++;
}
inline char pc(char ch){
if(p3 == obuf + (1 << 20)) flush();
*p3 = ch;
return *p3++;
}
template<typename type>
inline int rd(type &x){
x = 0; bool f = 0; char ch = gc();
while(!isdigit(ch)) f |= ch == '-', ch = gc();
while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
return f ? x = -x : 0;
}
template<typename type, typename ...T>
inline void rd(type &x, T &...y){rd(x), rd(y...);}
class Flush{public: ~Flush(){flush();}}___;
template<typename type>
inline void wr(type x){
if(x < 0) pc('-'), x = -x;
if(x > 9) wr(x / 10);
pc(x % 10 + '0');
return;
}
inline void wrs(const string& s){for(auto ch : s) pc(ch);}
namespace Solution{
const int INF = -1e18;
int T, n, a[100005], sum[100005], dp[100005][6][16][2];
bool vis[100005][6][16][2];
inline int dfs(int i, int x, int s, int o){
int j = i + x;
if(x >= 6){
if(!o){
int ans = sum[n] - sum[j], cur = 0, p = i;
fo(k, i + 1, j - 1){
if(!((s >> (j - k - 1)) & 1)){
if(k - p > 4) break;
cur += a[k], p = k;
}
}
return ans - cur;
}
int res = INF;
bool flag = false;
fo(d, 1, 4){
int newi = i + d;
if(newi < j && !((s >> (x - d - 1)) & 1)){
res = max(res, a[newi] - dfs(newi, x - d, s & ((1 << (x - d - 1)) - 1), 0));
flag = true;
}
}
if(!flag) res = -dfs(i, x, s, 0);
return res;
}
if(vis[i][x][s][o]) return 0;
if(dp[i][x][s][o] != INF) return dp[i][x][s][o];
vis[i][x][s][o] = true;
bool flag = false;
if(o){
fo(d, 1, 4){
int newi = i + d;
if(newi <= n && newi != j){
if(newi < j){
if(!((s >> (x - d - 1)) & 1)){
dp[i][x][s][o] = max(dp[i][x][s][o], a[newi] - dfs(newi, x - d, s & ((1 << (x - d - 1)) - 1), !o));
flag = true;
}
}else{
dp[i][x][s][o] = max(dp[i][x][s][o], a[newi] - dfs(j, newi - j, 0, o));
flag = true;
}
}
}
}else{
fo(d, 1, 4){
int newj = j + d;
if(newj <= n){
dp[i][x][s][o] = max(dp[i][x][s][o], a[newj] - dfs(i, newj - i, (s << d) | (1 << (d - 1)), !o));
flag = true;
}
}
}
if(!flag) dp[i][x][s][o] = max(dp[i][x][s][o], -dfs(i, x, s, !o));
vis[i][x][s][o] = false;
return dp[i][x][s][o];
}
inline void Solve(){
rd(T); while(T--){
rd(n); fo(i, 3, n) rd(a[i]), sum[i] = sum[i - 1] + a[i];
fo(i, 1, n) fo(x, 0, 5) fu(s, 0, 16) fo(o, 0, 1) dp[i][x][s][o] = INF, vis[i][x][s][o] = 0;
wr(dfs(1, 1, 0, 1)), pc('\n');
}
return;
}
}
bool ED;
signed main(){
clock_t START = clock();
// freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
Solution::Solve();
cerr << (double)(clock() - START) / CLOCKS_PER_SEC << " s" << '\n';
cerr << 1.0 * abs(&ED - &ST) / 1024 / 1024 << " MB" << '\n';
return 0;
}
:::