[YC703]ゴミ拾い Easy
[YC703]ゴミ拾い Easy
题目大意:
二维平面内有\(n(n\le3\times10^5)\)个人和\(n\)个物品,第\(i\)个人在\((a_i,0)\)上,第\(i\)个物品在\((x_i,y_i)(0\le a_i,x_i,y_i\le10^5)\)上,满足\(a_i<a_{i+1},x_i<x_{i+1}\)。每个人可以取走一些物品或者一个也不取。一个人取走物品\(j\sim i\)的代价为这个人到物品\(j\)距离的平方。每个人不能取比自己编号大的物品,问所有物品都被取完的最小代价。
思路:
用\(f[i]\)表示前\(i\)个人取走前\(i\)个物品的最小代价,一个显然的DP为:\(f[i]=\min\limits_{0\le j<i}\{f[j]+(x_{j+1}-a_i)^2+y_{j+1}^2\}\)。
将\(\min\)中间的展开,就是\(-2x_{j+1}a_i+x_{j+1}^2+y_{j+1}^2+f[j]\)。可以看作是一个关于\(a_i\)的一次函数。使用李超树维护一次函数最小值即可。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#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;
}
using int64=long long;
const int N=3e5+1,LIM=1e5;
int a[N],x[N],y[N];
int64 f[N];
using Line=std::pair<int64,int64>;
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
Line node[LIM<<2];
int64 calc(const Line &l,const int &x) const {
return l.first*x+l.second;
}
public:
void build(const int &p,const int &b,const int &e) {
node[p]={0,LLONG_MAX};
if(b==e) return;
build(p _left,b,mid);
build(p _right,mid+1,e);
}
void insert(const int &p,const int &b,const int &e,Line l1) {
if(node[p].second==LLONG_MAX) {
node[p]=l1;
return;
}
Line l2=node[p];
if(calc(l2,b)>=calc(l1,b)) std::swap(l1,l2);
if(calc(l1,e)>=calc(l2,e)) {
node[p]=l2;
return;
}
if(b==e) return;
const double c=1.*(l2.second-l1.second)/(l1.first-l2.first);
if(c<=mid) {
node[p]=l1;
insert(p _left,b,mid,l2);
} else {
node[p]=l2;
insert(p _right,mid+1,e,l1);
}
}
int64 query(const int &p,const int &b,const int &e,const int &x) const {
int64 ret=calc(node[p],x);
if(b==e) return ret;
if(x<=mid) ret=std::min(ret,query(p _left,b,mid,x));
if(x>mid) ret=std::min(ret,query(p _right,mid+1,e,x));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) a[i]=getint();
for(register int i=1;i<=n;i++) x[i]=getint();
for(register int i=1;i<=n;i++) y[i]=getint();
t.build(1,1,LIM);
for(register int i=1;i<=n;i++) {
t.insert(1,1,LIM,(Line){-2*x[i],(int64)x[i]*x[i]+(int64)y[i]*y[i]+f[i-1]});
f[i]=t.query(1,1,LIM,a[i])+(int64)a[i]*a[i];
}
printf("%lld\n",f[n]);
return 0;
}
[YC703]ゴミ拾い Easy的更多相关文章
- 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...
- 从零开始山寨Caffe·拾贰:IO系统(四)
消费者 回忆:生产者提供产品的接口 在第捌章,IO系统(二)中,生产者DataReader提供了外部消费接口: class DataReader { public: ......... Blockin ...
- CSS魔法堂:重拾Border之——更广阔的遐想
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——不仅仅是圆角
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——图片作边框
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——解构Border
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- Struts2 easy UI插件
一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...
- Easy UI常用插件使用
一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
随机推荐
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- Travelling(HDU3001+状压dp+三进制+最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目: 题意:n个城市,m条边,每条边都有一个权值,问你经过所有的城市且每条边通过次数不超过两次 ...
- NYOJ 1272 表达式求值 第九届省赛 (字符串处理)
title: 表达式求值 第九届省赛 nyoj 1272 tags: [栈,数据结构] 题目链接 描述 假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式. 2. 如果 X 和 Y 是 表 ...
- 21、利用selenium进行Web测试
一.案例实施步骤思路分析 1.寻包 2.指定浏览器(实例化浏览器对象) 3.打开项目 4.找到元素(定位元素) 5.操作元素 6.暂停 7.关闭二.元素定位[重点] 1.id 说明:通过元素的id属性 ...
- python3学习笔记.2.基础
1.编码 默认编码是 utf-8 # -*- coding: utf-8 -*- 2.注释 单行注释 # 多行注释,用三个单引号或双引号 3.关键字 可在交互窗口查询. >>> i ...
- flask基础之安装和使用入门(一)
前言 Flask框架作为一个python极简化的web框架,它不像Django那样的重型,非常适合快速开发一些小型的应用.本人用flask开发了几个项目之后,慢慢研究flask底层的一些原理,开始一步 ...
- 【UOJ#38】【清华集训2014】奇数国
考虑欧拉函数的性质,60很小,压位存下线段树每个节点出现质数. #include<bits/stdc++.h> ; ; typedef long long ll; using namesp ...
- 生成器(generator)和迭代(iterable , iterator, iteration)
在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...
- Freemaker如何遍历key为non-string类型的map?
(一) 前置知识 Freemaker默认配置下会使用SimpleHash去包装后台传递的hashmap,下段摘抄自官方reference 同样,当你传递进去一个hashmap实例时,会替换为一个sim ...
- Django 1.10文档中文版Part3
目录 2.7 第一个Django app,Part 5:测试 2.7.1 自动化测试介绍 2.7.2 基本的测试策略 2.7.3 编写我们的第一个测试程序 2.7.4 测试一个视图 2.7.5 测试越 ...