[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. 【leetcode 简单】第三十六题 最小栈

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...

  2. 【leetcode 简单】第十三题 最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  3. 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)

    题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...

  4. JS设计模式——5.单体模式(用了这么久,竟全然不知!)

    单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? 1.可以用它来划分命名空间(这个就是就是经常用的了) 2.利用分支技术来封装浏览器之间的差异(这个还真没用过,挺新鲜) 3.借 ...

  5. OGG相关操作

    参数文件详解: 1)truncate ogg 进程: Manager进程:manager进程是配置在源端和目标端 Extract(抽取)进程:部署在源端,用于捕获数据表或者日志中的数据文件: Pump ...

  6. Python标准库笔记(5) — sched模块

    事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...

  7. Linux下查看进程占用内存的最好方式

    今天看到stackoverflow上关于linux下如何查看某个进程占用的内存是多少的回答,觉得非常棒,不过是全英文的,很多人可能看不懂,所以我翻译一下 翻译自http://stackoverflow ...

  8. 71.Adam Taylor玩转MicroZed系列第82部分:简单通信接口第2部分

    By Adam Taylor 从上周的博客开始,我们已经进入到Zedboard(而不是MicroZed)板上的OLED显示模块的编程了.然而在正式进入具体的OLED编程之前,我认为有必要验证我们是否已 ...

  9. NFS挂载报如下错误信息:mount.nfs: Stale NFS file handle解决

    1)用fuser杀掉占用那个目录的进程 linux:~ # fuser -k /home/msgplus/msgplus/remote_dir 2)强制umount linux:~ # umount ...

  10. python3.x的HTMLTestRunner.py文件

    """A TestRunner for use with the Python unit testing framework. Itgenerates a HTML re ...