[CSAcademy]Squared Ends

题目大意:

给你一个长度为\(n(n\le10^4)\)的数列\(\{A_i\}(A_i\le10^6)\)。定义区间\(A_{[l,r]}\)的代价为\((A_l-A_r)^2\)。求将\(\{A_i\}\)划分成\(k(k\le100)\)个区间的最小代价。

思路:

不难想到一种动态规划,用\(f[i][j]\)表示已经划分了\(i\)个区间,结尾是\(j\)的最小代价。转移方程为:

\[f[i][j]=\min\{f[i-1][k-1]+(A_j-A_k)^2\}
\]

时间复杂度是\(\mathcal O(n^2k)\)。

变形得:

\[f[i][j]=A_j^2+\min\{-2A_jA_k+f[i-1][k-1]+A_k^2\}
\]

其中\(\min\)中的东西可以看做是关于\(A_j\)的一次函数。而寻找\(\min\)值的过程就相当于在一堆一次函数中找最小值,用李超树维护凸壳即可。

时间复杂度\(\mathcal O(nk\log\operatorname{range}(A_i))\)。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e4+1,K=101,LIM=1e6;
typedef long long int64;
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
struct Node {
int64 a,b,time;
};
Node node[LIM<<2];
public:
void reset(const int &p,const int &b,const int &e) {
node[p]=(Node){0,0};
if(b==e) return;
reset(p _left,b,mid);
reset(p _right,mid+1,e);
}
void insert(const int &p,const int &b,const int &e,const int64 &i,const int64 &j,const int &t) {
if(node[p].time!=t) {
node[p].a=i;
node[p].b=j;
node[p].time=t;
return;
}
const int64 lval1=node[p].a*b+node[p].b;
const int64 rval1=node[p].a*e+node[p].b;
const int64 lval2=i*b+j,rval2=i*e+j;
if(lval1<=lval2&&rval1<=rval2) return;
if(lval2<=lval1&&rval2<=rval1) {
node[p].a=i;
node[p].b=j;
return;
}
if(b==e) return;
const long double c=1.*(node[p].b-j)/(i-node[p].a);
if(lval1<=lval2&&c<=mid) {
insert(p _left,b,mid,node[p].a,node[p].b,t);
node[p].a=i;
node[p].b=j;
return;
}
if(lval1<=lval2&&c>=mid) {
insert(p _right,mid+1,e,i,j,t);
return;
}
if(lval1>=lval2&&c<=mid) {
insert(p _left,b,mid,i,j,t);
return;
}
if(lval1>=lval2&&c>=mid) {
insert(p _right,mid+1,e,node[p].a,node[p].b,t);
node[p].a=i;
node[p].b=j;
return;
}
}
int64 query(const int &p,const int &b,const int &e,const int &x,const int &t) const {
if(node[p].time!=t) return LLONG_MAX;
int64 ret=node[p].a*x+node[p].b;
if(b==e) return ret;
if(x<=mid) ret=std::min(ret,query(p _left,b,mid,x,t));
if(x>mid) ret=std::min(ret,query(p _right,mid+1,e,x,t));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int a[N];
int64 f[K][N];
int main() {
const int n=getint(),k=getint();
for(register int i=1;i<=n;i++) {
a[i]=getint();
f[0][i]=INT_MAX*500ll;
}
t.reset(1,1,n);
for(register int i=1;i<=k;i++) {
for(register int j=i;j<=n+i-k;j++) {
t.insert(1,1,LIM,-2*a[j],f[i-1][j-1]+(int64)a[j]*a[j],i);
f[i][j]=(int64)a[j]*a[j]+t.query(1,1,LIM,a[j],i);
}
}
printf("%lld\n",f[k][n]);
return 0;
}

[CSAcademy]Squared Ends的更多相关文章

  1. csa Round #70

    Digit Holes Time limit: 1000 msMemory limit: 256 MB   When writing digits, some of them are consider ...

  2. 关于cout<<ends你不知道的那些事

    关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...

  3. Gym 100650H Two Ends DFS+记忆化搜索

    Problem H: Two EndsIn the two-player game “Two Ends”, an even number of cards is laid out in a row. ...

  4. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  5. mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉

    当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...

  6. POJ 2738 Two Ends(记忆化)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  7. CSAcademy Beta Round #5 Force Graph

    题目链接:https://csacademy.com/contest/arhiva/#task/force_graph/ 大意是有若干个节点,每个节点对应一个二维坐标,节点之间相互有斥力存在.同时有些 ...

  8. CSAcademy Beta Round #5 Long Journey

    题目链接:https://csacademy.com/contest/arhiva/#task/long_journey/ 大意是有一张无向不带权的图,两个人同时从s点出发,分别前往a点和b点,且每个 ...

  9. CSAcademy Beta Round #4 Swap Pairing

    题目链接:https://csacademy.com/contest/arhiva/#task/swap_pairing/ 大意是给2*n个包含n种数字,每种数字出现恰好2次的数列,每一步操作可以交换 ...

随机推荐

  1. jQuery.pin.js笔记

    jQuery.pin.js是一个把元素钉在页面上某个位置的插件,它能够将某个元素一直挂在一个固定的位置而不论滚动条是否滚动. 特点: 1. 可以钉住一个元素,主要作用就是滚动超出的时候不会隐藏而是一直 ...

  2. Django之组合搜索组件(一)

    什么是组合搜索呢? 比如你想买车,但手里只有10万块!所以你只能在10万块的车里挑选,但你喜欢黑色,因为觉得很高端大气上档次,说白了就是装逼杠杠的!之后售车姐给你拿了个表表,你看到了低于10万块且颜色 ...

  3. 【译】第五篇 SQL Server代理理解代理错误日志

    本篇文章是SQL Server代理系列的第五篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.在第四篇中我们看到 ...

  4. Window 平台安装 Python:

    Window 平台安装 Python: 打开WEB浏览器访问http://www.python.org/download/ 在下载列表中选择Window平台安装包,包格式为:python-XYZ.ms ...

  5. Metlnfo CMS全版本漏洞收集

    根据https://www.seebug.org/appdir/MetInfo 进行书写. [版本:Metlnfo 4.0] 漏洞标题:Metlnfo cms任意用户密码修改 漏洞文件:member/ ...

  6. MySQL5.6 新特性之GTID【转】

    转自 MySQL5.6 新特性之GTID - jyzhou - 博客园http://www.cnblogs.com/zhoujinyi/p/4717951.html 背景: MySQL5.6在5.5的 ...

  7. 安装 Xamarin for Visual Studio

    总得来说,Xamarin 有“联网自动安装”和“手动安装”两种方式. 说明:本文涉及得资源链接都是官网的,同时,在 我的网盘 也有相关备份. 现在,我就以 Windows 为例来大概说明……(-=-我 ...

  8. Python 正则表达式、re模块

    一.正则表达式 对字符串的操作的需求几乎无处不在,比如网站注册时输入的手机号.邮箱判断是否合法.虽然可以使用python中的字符串内置函数,但是操作起来非常麻烦,代码冗余不利于重复使用. 正则表达式是 ...

  9. 动态更新echart成交量柱状图,并且不重绘,类似K线的更新方式

    function setoption(data) { let dataVolume=volumeChartData; var option = { title: { text: '成交量',// su ...

  10. linux系统性能排查命令

    [top] 命令可以动态查看当前系统的资源情况,以及占用资源的命令列表 用法: - ctrl + c / q : 停止此命令运行 - c : 展示完整的命令 - [top -bn1]:可以不动态的展示 ...