单调队列优化 dp
The only difference between easy and hard versions is the constraints.
Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of n
consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the i-th picture has beauty ai
.
Vova wants to repost exactly x
pictures in such a way that:
- each segment of the news feed of at least k
- consecutive pictures has at least one picture reposted by Vova;
- the sum of beauty values of reposted pictures is maximum possible.
For example, if k=1
then Vova has to repost all the pictures in the news feed. If k=2
then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.
Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.
The first line of the input contains three integers n,k
and x (1≤k,x≤n≤5000
) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.
The second line of the input contains n
integers a1,a2,…,an (1≤ai≤109), where ai is the beauty of the i
-th picture.
Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.
Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.
5 2 3
5 1 3 10 1
18
6 1 5
10 30 30 70 10 10
-1
4 3 1
1 100 1 1
100
题意 : 给你 n 个数字,要求从中选出 x 个数字,但任意连续的长度为 k 的区间中必须至少选择一个元素,询问所选择元素的最大的和是多少?
思路分析 :
定义 dp[i][j] 表示前 i 个树中选择 j 个数的最大得分
代码示例 :
n = 200
#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; ll n, k, x;
ll a[205];
ll dp[205][205]; void solve() {
memset(dp, -1*inf, sizeof(dp));
//printf("%lld ++++\n", dp[0][0]);
dp[0][0] = 0;
for(ll i = 1; i <= n; i++){
for(ll j = max(0ll, i-k); j <= i-1; j++){
for(ll f = 1; f <= x; f++){
dp[i][f] = max(dp[i][f], dp[j][f-1]+a[i]);
//prllf("++++ %d %d %d %d\n", i, j, f, dp[i][f]);
}
}
} ll ans = -1;
for(ll i = n; i > n-k; i--) ans = max(ans, dp[i][x]);
printf("%lld\n", ans);
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> k >> x;
for(ll i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
solve();
return 0;
}
n = 5000
#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;
typedef pair<ll, ll> P; // pos val
#define fi first
#define se second ll n, k, x;
ll a[5005];
deque<P>que[5005];
ll dp[5005][5005]; void solve() {
memset(dp, -1*inf, sizeof(dp));
dp[0][0] = 0;
que[0].push_back(P(0, 0));
ll ans = -1; for(ll i = 1; i <= n; i++){
ll pos = max(i-k, 0ll);
for(ll j = 1; j <= x; j++){
while(!que[j-1].empty() && que[j-1].front().fi < pos){
que[j-1].pop_front();
}
}
for(ll j = 1; j <= x; j++){
if (!que[j-1].empty()) {
ll p = que[j-1].front().fi;
ll val = que[j-1].front().se;
dp[i][j] = max(dp[i][j], dp[p][j-1]+a[i]);
//printf("^^^^^^^^^^^ %lld %lld %lld ++++ %lld %lld %lld\n", i, j, dp[i][j], p, j-1, dp[p][j-1]);
}
}
for(ll j = 1; j <= x; j++){
while(!que[j].empty() && dp[i][j] >= que[j].back().se) que[j].pop_back();
if (dp[i][j] > 0) que[j].push_back(P(i, dp[i][j]));
if (i > n-k) ans = max(ans, dp[i][j]);
}
//for(ll j = 1; j <= x; j++) {
//printf("++++ %lld %lld %lld\n", i, j, dp[i][j]);
//}
}
printf("%lld\n", ans);
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> k >> x;
for(ll i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
solve();
return 0;
}
单调队列优化 dp的更多相关文章
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- bzoj1855: [Scoi2010]股票交易--单调队列优化DP
单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP
BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...
- 【单调队列优化dp】 分组
[单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...
- [小明打联盟][斜率/单调队列 优化dp][背包]
链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...
- 单调队列以及单调队列优化DP
单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...
- BZOJ1791[Ioi2008]Island 岛屿 ——基环森林直径和+单调队列优化DP+树形DP
题目描述 你将要游览一个有N个岛屿的公园.从每一个岛i出发,只建造一座桥.桥的长度以Li表示.公园内总共有N座桥.尽管每座桥由一个岛连到另一个岛,但每座桥均可以双向行走.同时,每一对这样的岛屿,都有一 ...
- P4381 [IOI2008]Island(基环树+单调队列优化dp)
P4381 [IOI2008]Island 题意:求图中所有基环树的直径和 我们对每棵基环树分别计算答案. 首先我们先bfs找环(dfs易爆栈) 蓝后我们处理直径 直径不在环上,就在环上某点的子树上 ...
随机推荐
- P1094 百钱白鸡
题目描述 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买100只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱. 输入格式 无. 输出格式 输出所有 ...
- tomcat最大线程数的设置
Tomcat的server.xml中连接器设置如下 <Connector port="8080" maxThreads="150" minSpareThr ...
- vuex 快速上手,具体使用方法总结(含使用例子)
网上有关vuex的文章很多,但有些比较复杂,这篇文章能让你快速使用vuex: vuex 用处:管理全局状态(类似全局变量,每个组件都能访问到) vuex 用法: //下面是一个js文件,用最简单最全的 ...
- iview table中的render函数使用
1.表格列数据内容过多可以用以下两个属性解决: ellipsis:"true', tooltip:true 使每个列的内容如果过多的话变为省略号 2.table中的render函数(实现根据 ...
- Linux 内核设备属性
sysfs 中的设备入口可有属性. 相关的结构是: struct device_attribute { struct attribute attr; ssize_t (*show)(struct de ...
- boostrap-非常好用但是容易让人忽略的地方【3】:clearfix
代码 显示结果 代码 结果
- Keras cnn 手写数字识别示例
#基于mnist数据集的手写数字识别 #构造了cnn网络拟合识别函数,前两层为卷积层,第三层为池化层,第四层为Flatten层,最后两层为全连接层 #基于Keras 2.1.1 Tensorflow ...
- slim中的请求头
请求头 每个 HTTP 请求都有请求头.这些元数据描述了 HTTP 请求,但在请求体中不可见.Slim 的 PSR 7 请求对象提供了几个检查请求头的方法. 获取所有的请求头,返回一个数组:getHe ...
- vue新增属性响应式更新的问题
根据官方文档定义: 如果在实例创建之后添加新的属性到实例上,它不会触发视图更新. 受现代 JavaScript 的限制 (以及废弃 Object.observe),Vue 不能检测到对象属性的添加或删 ...
- ansible批量部署mysql
1.1 将mysql软件包同步到客户端服务器,做安装前期准备 Ps:mysql安装包线拖到ansible服务端的/ansible/roles/mysql/files目录下 vim /ansible/r ...