HDU-5900 QSC and Master
题目大意:
有n对二元组(key, value),两个相邻的元组间如果key的不互质,那么可以被移除,并获得两个元组的value值之和的分数,问你最多能有多少分数。
解题思路:
区间DP
按照最裸的区间DP模型用记忆化搜索写是要超时的...
本题的模型可以参考POJ-2955 Brackets题解这里有~
设dp[i][j]表示区间[i, j]能获得的最大分数状态转移就可以写成
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j], dp[i+1][j-1] + value[i] + value[j]);
剩下的就很简单了。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;typedef long long LL;
const int maxn = 305;bool judge[maxn][maxn];
pair<int, int> p[maxn];
LL sum[maxn], dp[maxn][maxn];LL gcd(LL a, LL b) {while (b) {LL tmp = a % b;a = b;b = tmp;}return a;
}int main() {int n, t;scanf("%d", &t);while (t--) {scanf("%d", &n);sum[0] = 0;for (int i = 1; i <= n; ++i)scanf("%d", &p[i].first);for (int i = 1; i <= n; ++i) {scanf("%d", &p[i].second);sum[i] = sum[i - 1] + p[i].second;}memset(judge, false, sizeof(judge));for (int i = 1; i <= n; ++i) {for (int j = i + 1; j <= n; ++j)judge[i][j] = (gcd(p[i].first, p[j].first) == 1 ? false : true);}for (int i = n; i >= 1; --i) {for (int j = i; j <= n; ++j) {dp[i][j] = 0;for (int k = i; k < j; ++k)dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);if ((i + 1 == j || dp[i + 1][j - 1] == sum[j - 1] - sum[i]) && judge[i][j])dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + p[i].second + p[j].second);}}printf("%lld\n", dp[1][n]);}return 0;
}
转载于:https://www.cnblogs.com/wiklvrain/p/8179353.html
HDU-5900 QSC and Master相关推荐
- hdu-5900 QSC and Master(区间dp)
题目链接: QSC and Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 【HDU - 5900】QSC and Master(区间dp)
题干: Every school has some legends, Northeastern University is the same. Enter from the north gate of ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- 2016 ACM/ICPC Asia Regional Shenyang Online
I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...
- ACM/ICPC2016沈阳网络赛(不完全)解题报告
比赛地址: http://acm.hdu.edu.cn/contests/contest_show.php?cid=724 1003.hannnnah_j's Biological Test 题目大意 ...
- 2016.9.18 --- Shenyang ol
1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j's Biological Test 1004 Mathematician QS ...
- HDU 5308 I Wanna Become A 24-Point Master
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5308 题面: I Wanna Become A 24-Point Master Time Limit ...
- HDU - 7125 Master of Shuangpin
HDU - 7125 AYIT609第一周周赛(2021) Problem Description As you know, there are three kinds of Chinese inpu ...
- HDU 6709“Fishing Master”(贪心+优先级队列)
传送门 •参考资料 2019CCPC网络选拔赛 H.Fishing Master(思维+贪心) •题意 池塘里有 n 条鱼,捕捉一条鱼需要花费固定的 k 时间: 你有一个锅,每次只能煮一条鱼,其中煮熟 ...
最新文章
- SaltStack(五) SaltStack与ZeroMQ
- c#排序算法(待续)
- AS3.0 位图翻转、旋转
- linux内外部命令,Shell、内外部命令――Linux基本命令(2)
- api restful_HATEOAS的RESTful服务。 记录超媒体API
- jquery手写轮播图_15个超强的jQuery/HTML5图片轮播插件
- python进行usb通讯_Python实现树莓派USB串口通讯
- Android笔记 actionbar学习
- 【HISI系列】之Hi3559A V100R001C02SPC010文档及SDK
- matlab中的myerr,Error in 'MPC1/S-Function' while executing MATLAB S-function 'MY_MPCCon...
- 人工智能应用在会计工作中的优势
- 程序名称:ROS强开首页(类似电信ADSL强开指定首页)
- SQL基础知识总结(SQL必知必会)
- [计算机网络】【网络设备】虚拟局域网
- 鲲鹏Devkit代码迁移工具课堂总结
- 学术论文投稿第五弹——如何写Discussion
- 回头再说--英雄 汪峰
- Linux下安装JDK教程,老爷爷看了都说会了!
- 第9节 蒙卡模拟计算美式期权价格(b)
- 无线hacking系统—wifislax
热门文章
- RSA非对称加密算法之公钥和私钥详细介绍
- JZOJ 5490. 【清华集训2017模拟11.28】图染色
- outlook 单独安装_民强村排烟管道安装公司
- 电脑屏幕保护怎么取消_怎么监控员工电脑?老板如何知道员工上班在干什么?...
- md5碰撞Java_java现在MD5加密不安全了吗?
- 数学之美系列之一:统计语言模型 (Statistical Language Models)
- Faster R-CNN 《Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks》论文笔记
- 通信系统概论_移动通信复习资料
- am5728 是否支持aarch64_am5728开启uart0接口通讯
- python web开发项目 源码_真零基础Python开发web