文章目录

  • 题面
  • 题解

题面

传送门

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. android的init过程分析
  2. 热传的职场异性相处PPT!网友评:你倒是给我分配个女同事啊!
  3. git只提交一张图片_Git 图形化操作之合并提交记录
  4. python的pygame游戏开始结束信息_从0开始学python第14.8节-pygame射击游戏(一)
  5. node JS 微信开发
  6. 菜鸟修炼C语言小设计之——工资统计
  7. 图的建立-邻接表表示(C语言)
  8. oracle 索引 lob 迁移,Oracle 11g到19c迁移TB级lob表的酸爽
  9. c#进阶之lambda表达式
  10. 爬虫项目——BS练手(2)
  11. 微信小程序项目源代码精品微信小程序电子书城销售系统|商城|电商系统
  12. 服务器装系统就蓝屏,重装了下系统老是蓝屏
  13. u盘efi分区删除方法
  14. MT6765/MT6762/MT6761平台能否使用ACC/Gyro/A+G与AP之间的I2C接口
  15. 删除的android电话怎么找回,通话记录删除了怎么恢复?安卓手机通话记录恢复方法...
  16. 乐视x820android最新版本,乐视 Max2 Android 10更新教程
  17. StringBuilder和输入输出
  18. 银河麒麟批量压缩图片的方法
  19. 关于 /proc/mounts
  20. java计算机毕业设计ssm洗浴管理系统l9omz(附源码、数据库)

热门文章

  1. ubuntu 20.04 安装 keepass【解决中文乱码】
  2. 博科brocade SAN光纤交换机端口激活过程(新版)
  3. 2019年软考及格分数线
  4. a:link、a:visited、a:hover、a:active伪类选择器
  5. 成本对象(生产订单)控制
  6. 无线路由器故障排除与解决方案
  7. python 小说人物分析_Python语言之用Python分析一下当年看金庸小说最爱的主角是谁...
  8. 计算机科学与技术教师简介,清华大学计算机科学与技术系导师教师师资介绍简介-应明生...
  9. 全球健康药物研发中心郭晋疆:多元科学计算系统在药物研发管线中的搭建与实践
  10. 【问题解决方案】电脑使用微信等第三方工具给鼠标右键和下拉列表截图