[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.cookie.js——jquery的cookie插件

    一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...

  2. C# 动态调取 soap 接口

    调用示例 string url = "http://localhost:8080/server/PatientService.asmx"; Hashtable ht = new H ...

  3. layui的模块化和非模块化使用

    非模块化和模块化的区别是 非模块化不用每次都调用layui.use([],fun...)引入对应模块,引入的JS是/layui/layui.all.js 模块化必须每次都调用layui.use([], ...

  4. 33、求按从小到大的顺序的第N个丑数

    一.题目 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 二.解法 ...

  5. ubuntu12.04 svn ssl错误

    1,ubuntu12.04 svn ssl错误提示: OPTIONS of '<url>': SSL handshake failed: SSL error: Key usage viol ...

  6. fastDFS 命令笔记【转】

    端口开放 这是命令运行的前提 iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT iptables -I I ...

  7. PHP缓存加速插件 XCache 、 ZendOpcache 安装

    PHP缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(OperateCode,简称opcode)文件,该文件是PHP代码的一种二进制表示方式.默 ...

  8. 安全控制 iptables

    转自:http://www.opsers.org/linux-home/videos/chapter-netfilter-iptables-raiders.html 内容简介防火墙的概述iptable ...

  9. python爬取网易云音乐歌单音乐

    在网易云音乐中第一页歌单的url:http://music.163.com/#/discover/playlist/ 依次第二页:http://music.163.com/#/discover/pla ...

  10. <<Javascript Patterns>>阅读笔记 -- 第2章 基本技巧(二)

    关于for-in循环 循环数据时, 强烈不推荐使用for-in循环.因为当Array对象被扩展后, 再用for-in循环遍历数据会导致逻辑上的错误, 举例说明: var arr = ['a', 'b' ...