文章目录

  • 题面
  • 题解

题面

传送门

A sequence of integer numbers is called strictly monotonically increasing if every term of the sequence is strictly greater than the one preceding it. Similarly, a sequence is called strictly monotonically decreasing if every term is strictly less than the one preceding it. A strictly monotonic sequence is a sequence that is either strictly monotonically increasing or decreasing. A sequence of integers is called k-monotonic if it can be decomposed into k disjoint contiguous subsequences that are strictly monotonic.
.
For example a strictly monotonically increasing sequence is 1-monotonic — in fact it is k-monotonic for every k between 1 and the number of elements it contains. The sequence { 1, 2, 3, 2, 1 } is 2-monotonic since it can be decomposed into { 1, 2, 3 } and { 2, 1 }.
.
If a sequence is not k-monotonic, you can transform it into a k-monotonic sequence by performing the following operation one or more times: select any term in the sequence and either increase it or decrease it by one. You are allowed to perform any number of these operations on any of the terms. Given a sequence of numbers A1, A2, …, An and an integer k, you are to calculate the minimum number of operations required to transform the given sequence into a k-monotonic sequence.

题解

题意
给定一个数组,定义区间 [ l , r ] [l,r] [l,r] K K K单调为,区间中,存在一种切割方式,将数组隔开为 K K K 个小区间,小区间内严格单调递增/递减
推到,如果一个长度是为 4 4 4​​ 的 1 1 1​ 单调区间,那么它也是 k , k ∈ { 2 , 3 , 4 } k,k\in\{ 2,3,4\} k,k∈{2,3,4}​单调区间

分析
考虑 1 1 1 单调的情况,求出区间 l l l ~ r r r 变为 1 1 1单调的代价,分为单调递增和单调递减两种
严格单调增,需要把 a [ i ] − = i a[i]−=i a[i]−=i 处理
求单调递减的时候,将 a [ i ] + = i a[i]+=i a[i]+=i​ 再取负,同样计算

遍历起点,计算所有的 l l l ~ r r r 的代价

最后,通过 d p dp dp 计算最后的结果

d p [ k ] [ i ] = min ⁡ ( d p [ k − 1 ] [ j ] + min ⁡ ( c o s t A [ j + 1 ] [ i ] , c o s t B [ j + 1 ] [ i ] ) ) dp[k][i]=\min(dp[k−1][j]+\min(costA[j+1][i],costB[j+1][i])) dp[k][i]=min(dp[k−1][j]+min(costA[j+1][i],costB[j+1][i]))

状态表示为,将前i个转化为k单调需要的代价

答案为 d p [ K ] [ N ] dp[K][N] dp[K][N]

//322971D
/*@Author: YooQ
*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define sc scanf
#define pr printf
#define ll long long
#define int long long
#define FILE_OUT freopen("out", "w", stdout);
#define FILE_IN freopen("in", "r", stdin);
#define debug(x) cout << #x << ": " << x << "\n";
#define AC 0
#define WA 1
#define INF 0x3f3f3f3f
const ll MAX_N = 1e6+5;
const ll MOD = 1e9+7;
int N, M, K;struct Tr{int k, l, r, dis;
}tr[MAX_N];
int indx = 0;int merge(int x, int y) {if (!x || !y) return x | y;if (tr[x].k < tr[y].k) swap(x, y);tr[x].r = merge(tr[x].r, y);if (tr[tr[x].l].dis <= tr[tr[x].r].dis) {swap(tr[x].l, tr[x].r);}tr[x].dis = tr[tr[x].r].dis + 1;return x;
}int mk(int x) {tr[++indx].k = x;tr[indx].dis = tr[indx].l = tr[indx].r = 0;return indx;
}void pop(int& rt) {rt = merge(tr[rt].l, tr[rt].r);tr[rt].dis = tr[tr[rt].r].dis + 1;
}int arr[MAX_N];
int brr[MAX_N];
int stk[MAX_N];
int sz[MAX_N];
int cnt[MAX_N];
int tt = 0;void init() {indx = 0;tt = 0;
} int costA[1005][1005];
int costB[1005][1005];void calc(int st) {init();int res = 0;for (int i = st; i <= N; ++i) {stk[++tt] = mk(arr[i]);cnt[tt] = sz[tt] = 1;while (tt-1 && tr[stk[tt-1]].k >= tr[stk[tt]].k) {if (sz[tt]&1) res += tr[stk[tt-1]].k - tr[stk[tt]].k;stk[tt-1] = merge(stk[tt-1], stk[tt]);sz[tt-1] += sz[tt];cnt[tt-1] += cnt[tt];--tt;while (cnt[tt] > (sz[tt]+1)/2) {pop(stk[tt]);--cnt[tt];}}costA[st][i] = res;}init();res = 0;for (int i = st; i <= N; ++i) {stk[++tt] = mk(brr[i]);cnt[tt] = sz[tt] = 1;while (tt-1 && tr[stk[tt-1]].k >= tr[stk[tt]].k) {if (sz[tt]&1) res += tr[stk[tt-1]].k - tr[stk[tt]].k;stk[tt-1] = merge(stk[tt-1], stk[tt]);sz[tt-1] += sz[tt];cnt[tt-1] += cnt[tt];--tt;while (cnt[tt] > (sz[tt]+1)/2) {pop(stk[tt]);--cnt[tt];}}costB[st][i] = res;}
} int dp[12][1005];void solve(){init();for (int i = 1; i <= N; ++i) {sc("%lld", &arr[i]);brr[i] = - (arr[i] + i);arr[i] -= i;}for (int i = 1; i <= N; ++i) {calc(i);}memset(dp, INF, sizeof dp);dp[0][0] = 0;for (int k = 1; k <= K; ++k) {for (int i = 1; i <= N; ++i) {for (int j = 1; j <= i; ++j) {dp[k][i] = min(dp[k][i], dp[k-1][j-1] + min(costA[j][i], costB[j][i]));}}}pr("%lld\n", dp[K][N]);
}signed main()
{#ifndef ONLINE_JUDGE//FILE_INFILE_OUT#endifint T = 1;//cin >> T;while (sc("%lld%lld", &N, &K), N, K) solve();return AC;
}

POJ-3016 K-Monotonic相关推荐

  1. POJ 3111 K Best 贪心 二分

    题目链接: http://poj.org/problem?id=3111 题目描述: 在N个物品中让你选出K个, 使得平均价值最大 解题思路: 这是我错的代码......一会儿回来改啊......一会 ...

  2. POJ 1325 Kőnig's Theorem

    http://poj.org/problem?id=1325 The minimum vertex cover bipartite graph According to the Kőnig's the ...

  3. POJ 3111 K Best (最大化平均值,贪心 二分)难度⭐⭐⭐

    题目来源: [题意] 有n个物品的重量和价值分别是wi,vi,从中选取k个物品使得单位重量的价值最大. 输出格式: 输出一行物品的编号. #include<iostream> #inclu ...

  4. Poj 2109 k^n = p.

    Poj2109(1)和Poj2109(2)这两种解答都是有漏洞的,就是解不一定存在. 当然这种漏洞的存在取决于出题人是否假设输入的n,p必须默认有kn = p这样的关系存在. 这道题可以详细看http ...

  5. 《挑战程序设计竞赛(第2版)》习题册攻略

    本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...

  6. GB_T28181-2016.pdf

    国标28181-2016版本检测,由于文件过大,而且博客不支持上传文件,需要GB28181-2016协议文档和公安一所检测文档的可以私信我,QQ:123011785 I C S1 3. 3 1 0 A ...

  7. K - Candies POJ - 3159(利用了自定义比较操作符)

    K - Candies POJ - 3159 题意: 孩子 A 觉得 B 得到的糖果不能比自己多超过 c,求 n 比 1 最多能多几颗糖果 思路:DJ,松弛条件: sweet[A] > swee ...

  8. K - 迷宫问题 POJ - 3984

    K - 迷宫问题 POJ - 3984 5*5迷宫,输出从(0,0)走到(4,4)的最短路径 类似康托,自己弄个一一对应的公式即可 #include<iostream> #include& ...

  9. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  10. poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)

    题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自 ...

最新文章

  1. 为centos选择国内yum软件库
  2. 跟着鸟哥学Linux系列笔记1
  3. β射线与哪些物质可产生较高的韧致辐射_辐射无所不在,香蕉土豆里都有?我们还能愉快生活吗?...
  4. 华为C8825D刷机失败解决方法
  5. 《AutoCAD 2016中文版从入门到精通》——1.5 基本输入操作
  6. C++标准转换运算符:const_cast
  7. 机器学习(5)——决策树(预测隐形眼镜类型)
  8. Django——日志
  9. [追加评论]三款SDR平台对比:HackRF,bladeRF和USRP
  10. 前端面试题总汇、常考、笔试题等
  11. VectorDraw web library javascript Crack
  12. ug添加imachining变量_UG变量设置
  13. android9支持的tf卡格式,老手机福音 三星安卓9.0测试存储卡装应用功能
  14. linux系统接显示器 扩展模式怎么设置,Ubuntu10.04双显示器扩展方式 接显示器分辨率低的解决...
  15. 互动广告助您抢量成功
  16. 最全数据集网站汇总,绝对是一个金矿请查收!
  17. 老是抓不住伦敦黄金实时行情,怎么办?
  18. Android密钥证书学习
  19. takemehigher计算机乐谱,【图片】简谱【无饿不作吧】_百度贴吧
  20. java项目 升级jdk1.8 遇到 tomcat启动 are only available on JDK 1.5 and higher错误

热门文章

  1. 前端开发免费资源分享
  2. 经典英文爱情句子10条
  3. 设计模式演化之桥接模式
  4. MySQL之——CentOS下my.cnf 配置 日志类型及文件配置详解(我本人服务器上的配置,亲测可用)
  5. java 红包接口_微信支付中微信红包的接口测试,Java版本
  6. 做开发几年的个人经历
  7. 20150910互联网产业园_子页面(图文)
  8. 腾讯游戏数据自愈服务方案
  9. Java注解和反射,苦熬一个月
  10. 我国DTMB正式成为全球第四个数字电视国际标准