HDU 5900 QSC and Master(区间dp)
来源:互联网 发布:python genetic 编辑:程序博客网 时间:2024/04/30 21:08
代码:
#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;}
0 0
- hdu 5900 QSC and Master 区间dp
- hdu 5900 QSC and Master 区间dp
- HDU 5900 QSC and Master(区间dp)
- hdu 5900 QSC and Master【区间dp】
- Hdu-5900 QSC and Master(区间DP)
- HDU 5900 QSC and Master(区间DP)
- HDU 5900 QSC and Master(区间dp)
- HDU 5900 QSC and Master(区间DP)
- HDU 5900 QSC and Master 区间DP
- hdu 5900 QSC and Master 区间dp
- HDU-5900 QSC and Master(区间DP)
- HDU 5900 QSC and Master(区间DP)
- HDU 5900-QSC and Master - 区间 の dp
- HDU-5900-QSC and Master(区间DP)
- hdu 5900 QSC and Master (区间dp)
- HDU 5900 QSC and Master(区间DP)
- HDU 5900 QSC and Master (区间DP)
- HDU 5900 QSC and Master (区间DP)
- Max Sum(第一周J题)
- iOS中旋转加载动画的实现
- C#结束进程树
- 常用的shell命令
- java实现并发搜索数组元素
- HDU 5900 QSC and Master(区间dp)
- 双飞翼和圣杯布局
- SPS、PPS、IDR
- MD5加密
- 简单的imageloader
- SQL语言总结
- pojo的排序问题疑问
- 学习计划-16.09.21
- Convert Sorted Array to Binary Search Tree——Difficulty:Medium