bzoj3675: [Apio2014]序列分割
留坑
为什么别人家的斜率优化跟我一点都不一样!
为什么斜率都要变成正的。。。
为什么要那么推式子
为什么不能直接做啊。。。。。
为什么不把0去掉去秒WA啊
为什么叉积去了0也过不了啊
woc啊
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; void setIO(const string& s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<typename Q> Q read(Q& x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x;
return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} typedef long long LL;
const int N = + ; LL sqr(const LL& x) {
return x * x;
} LL f[N], g[N], sum[N];
int q[N]; LL up(int i) {
return g[i] - sqr(sum[i]);
} LL up(int i, int j) {
return up(j) - up(i);
} LL down(int i, int j) { // >= 0
return sum[j] - sum[i];
} double slope(int i, int j) {
return up(i, j) / (double) down(i, j);
}
int n; struct Node {
LL x, y;
Node() {}
Node(const LL& x, const LL& y) : x(x), y(y) {}
Node operator - (const Node& rhs) const {
return Node(x - rhs.x, y - rhs.y);
}
}p[N]; LL Cross(const Node& a, const Node& b) {
return a.x * b.y - b.x * a.y;
} void dp(int st) {
int L = , R = ;
q[R++] = st;
for(int i = st + ; i <= n; i++) {
// while(L + 1 < R && -sum[i] < slope(q[L], q[L + 1])) L++;
// while(L + 1 < R && -sum[i] * down(q[L], q[L + 1]) < up(q[L], q[L + 1])) L++;
while(L + < R && Cross(Node(, -sum[i]), p[L + ] - p[L]) >= ) L++;
int j = q[L];
f[i] = g[j] + sum[j] * (sum[i] - sum[j]);
// while(L + 1 < R && slope(q[R - 2], q[R - 1]) < slope(q[R - 2], i)) R--;
// while(L + 1 < R && up(q[R - 2], q[R - 1]) * down(q[R - 2], i) < up(q[R - 2], i) * down(q[R - 2], q[R - 1])) R--;
Node u(sum[i], up(i));
while(L + < R && Cross(p[R - ] - p[R - ], u - p[R - ]) >= ) R--;
q[R] = i, p[R++] = u;
}
for(int i = st + ; i <= n; i++) g[i] = f[i];
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif read(n);
int k = read<int>(); for(int i = ; i <= n; i++) {
int x = read<int>();
if(!x) i--, n--;
else sum[i] = sum[i-] + x;
// sum[i] = sum[i-1] + read<int>();
} for(int i = ; i <= k; i++) dp(i); printf("%lld\n", g[n]); return ;
}
bzoj3675: [Apio2014]序列分割的更多相关文章
- bzoj3675[Apio2014]序列分割 斜率优化dp
3675: [Apio2014]序列分割 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3508 Solved: 1402[Submit][Stat ...
- BZOJ3675 [Apio2014]序列分割 【斜率优化dp】
3675: [Apio2014]序列分割 Time Limit: 40 Sec Memory Limit: 128 MB Submit: 3366 Solved: 1355 [Submit][St ...
- [Bzoj3675][Apio2014]序列分割(斜率优化)
3675: [Apio2014]序列分割 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 4021 Solved: 1569[Submit][Stat ...
- BZOJ3675 [Apio2014]序列分割 动态规划 斜率优化
原文链接http://www.cnblogs.com/zhouzhendong/p/8697258.html 题目传送门 - BZOJ3675 题意 对于一个非负整数序列,小H需要重复k次以下的步骤: ...
- BZOJ3675 Apio2014 序列分割 【斜率优化】
Description 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列分割成k+1个非空的子序列.为了得到k+1个子序列,小H需要重复k次以下的步骤: 1.小H首 ...
- BZOJ3675: [Apio2014]序列分割(斜率优化)
Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 4186 Solved: 1629[Submit][Status][Discuss] Descript ...
- 2018.09.29 bzoj3675: [Apio2014]序列分割(斜率优化dp)
传送门 斜率优化dp经典题目. 首先需要证明只要选择的K个断点是相同的,那么得到的答案也是相同的. 根据分治的思想,我们只需要证明有两个断点时成立,就能推出K个断点时成立. 我们设两个断点分成的三段连 ...
- [luogu3648][bzoj3675][APIO2014]序列分割【动态规划+斜率优化】
题目大意 让你把一个数列分成k+1个部分,使分成乘积分成各个段乘积和最大. 分析 首先肯定是无法开下n \(\times\) n的数组,那么来一个小技巧:因为我们知道k的状态肯定是从k-1的状态转移过 ...
- 【BZOJ-3675】序列分割 DP + 斜率优化
3675: [Apio2014]序列分割 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1420 Solved: 583[Submit][Statu ...
随机推荐
- 网页解析Jsoup简单使用
public static void main(String[] args) throws IOException { //System.out.println("Hello World!& ...
- Html5学习--canvas
canvas元素的实际尺寸(宽度,高度)是由标签上的属性width和height来决定的. 这两个属性不设置时默认值为:宽度=300px,高度=150px. 如果在样式表里设置width和height ...
- javascript删除目标标签
<script> window.onload=function(){ var odiv=document.getElementById("content"); // ...
- postgresql 的触发器
今天编写了一个触发器 功能: 有两个表,当一个表的字段有所改动的时候,另一个表跟着改动 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS T ...
- PHPCMS实现文章置顶功能的方法
我个人喜欢把PHPCMS当作博客来用,而作为一个博客,怎能少了文章置顶功能呢?其中用PHPCMS实现置顶功能非常简单,无非是修改下推荐位的名称为置顶,然后在文章列表中推送需要置顶的文章罢了. 不过博客 ...
- 关于python decode()和 encode()
1.先收集一下这几天看到的关于decode()解码和encode()编码的用法 bytes和str是字节包和字符串,python3中会区分bytes和str,不会混用这两个.字符串可以编码成字节包,而 ...
- JavaScript 自定义单元测试
<!doctype html> <html> <head> <meta charset="utf-8"> <script> ...
- 2016021904 - 如何使用Memory Analyzer
如何使用Memory Analyzer呢? 0.有内存溢出的代码code.<深入理解java虚拟机>中代码 package neutron.oom.heap; import java.ut ...
- Python学习笔记(一)Python安装及环境变量的配置
1.下载python安装包. 下载地址:https://www.python.org/ 2.配置环境变量 找到python的安装路径.C:\Python27;script的路径:C:\Python27 ...
- [转]hive中order by,distribute by,sort by,cluster by
转至http://my.oschina.net/repine/blog/296562 order by,distribute by,sort by,cluster by 查询使用说明 1 2 3 4 ...