HDU 5900 QSC and Master(区间dp)

本文介绍了一个基于区间动态规划的游戏策略问题。玩家通过消除相邻且最大公约数不为1的数字来获取分数,目标是最大化总分。文章详细阐述了解决方案,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

n300keygcd=1
dp[l][r]=max(dp[l+1][i1]+b[l]+b[i]+dp[i+1][r]),gcd(a[l],a[i])!=1s[l+1][i1]=1
s[l][r]=1[l,r]
dp[l][r]=max(dp[l][r],dp[l+1][r])


代码:

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;
#define   MAX           1000005
#define   MAXN          6005
#define   maxnode       10
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
//const double inf   = 1e18;
const double eps   = 1e-8;
const LL     mod   = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
    char c;
    while((c=getchar())<'0' || c>'9');
    x=c-'0';
    while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

int a[305];
int b[305];
LL dp[305][305];
int s[305][305];

LL dfs(int l,int r){
    if(dp[l][r]!=-1) return dp[l][r];
    if(l==r){
        s[l][r]=-1;
        dp[l][r]=0;
        return dp[l][r];
    }
    if(l>r){
        s[l][r]=1;
        dp[l][r]=0;
        return dp[l][r];
    }
    if(l+1==r){
        if(__gcd(a[l],a[r])!=1){
            s[l][r]=1;
            dp[l][r]=b[l]+b[r];
        }
        else{
            s[l][r]=-1;
            dp[l][r]=0;
        }
        return dp[l][r];
    }
    LL ans=0;
    int flag=0;
    for(int i=l+1;i<=r;i++){
        LL tmp=dfs(l+1,i-1);
        if(s[l+1][i-1]==1&&__gcd(a[l],a[i])!=1){
            ans=max(ans,dp[l+1][i-1]+b[l]+b[i]+dfs(i+1,r));
            if(s[i+1][r]==1) flag=1;
        }
    }
    if(flag) s[l][r]=1;
    else s[l][r]=-1;
    ans=max(ans,dfs(l+1,r));
    return dp[l][r]=ans;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        mem(dp,-1);
        cout<<dfs(1,n)<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值