[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 ...
随机推荐
- NYOJ 1022 合纵连横 (并查集)
题目链接 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土面积不断扩大.而实力弱的诸侯王为了不让自己的领 ...
- 45、如何使用python删除一个文件?
若想利用python删除windows里的文件,这里需要使用os模块!那接下来就看看利用os模块是如何删除文件的! 具体实现方法如下! os.remove(path) 删除文件 path. 如果pat ...
- elasticsearch删除索引报错【原】
如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...
- PhantomJS、Selenium、Chrome驱动的mac版安装和配置
PhantomJS(爬取动态页面需要用到) 基于webkit的javaScript API.提供了css选择器,提供了处理文件的I/O操作,支持Web标准.DOM操作.JSON.HTML5.Canva ...
- ActiveMQ-Network of brokers集群模式
概述 在ActiveMQ运行过程中,如果发生某个queue只有生产者没有消费者的情况时,消息就会产生积压.Network of brokers模式通过将积压的消息转发给处于同一network的其它br ...
- caffe源码整个训练过程
Caffe源码 Blob protected: shared_ptr<SyncedMemory> data_; shared_ptr<SyncedMemory> diff_; ...
- Effective STL 笔记: Item 6--Be alert for C++'s most vexing parse
假设有个文件里面记录的一系列的 int 值,现在我们想把这些数值存到一个 List 里面,结合 Item 5, 我们可能会写出下面的代码: ifstream dataFile("ints.d ...
- 完美解决doc、docx格式word转换为Html
http://blog.csdn.net/renzhehongyi/article/details/48767597
- MINIBASE源代码阅读笔记之heapfile
Heapfile 用来管理heap file里的dir page们 成员 _firstDirPageId:这个文件的第一个dir page _ftype:文件类型 _file_deleted:删除的时 ...
- IEEEXtreme 10.0 - Always Be In Control
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Always Be In Control 题目来源 第10届IEEE极限编程大赛 https://www.h ...