[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的更多相关文章

  1. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  2. 从零开始山寨Caffe·拾贰:IO系统(四)

    消费者 回忆:生产者提供产品的接口 在第捌章,IO系统(二)中,生产者DataReader提供了外部消费接口: class DataReader { public: ......... Blockin ...

  3. CSS魔法堂:重拾Border之——更广阔的遐想

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  4. CSS魔法堂:重拾Border之——不仅仅是圆角

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  5. CSS魔法堂:重拾Border之——图片作边框

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  6. CSS魔法堂:重拾Border之——解构Border

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  7. Struts2 easy UI插件

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

  8. Easy UI常用插件使用

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

  9. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

随机推荐

  1. [SCOI2010]生成字符串 题解(卡特兰数的扩展)

    [SCOI2010]生成字符串 Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数 ...

  2. oracle数据库只查询前n条

    select * from  (select * from   tablename order by createdate desc)  aaa -- 按创建时间倒排序 where rownum &l ...

  3. Java并发编程(二)

    1.Lock接口                                                        在Lock接口出现之前,Java程序是靠synchronized关键字实 ...

  4. MySQL-索引工作原理及使用注意事项

    1.为什么需要索引(Why is it needed)? 当数据保存在磁盘类存储介质上时,它是作为数据块存放.这些数据块是被当作一个整体来访问的,这样可以保证操作的原子性.硬盘数据块存储结构类似于链表 ...

  5. No manual entry for pthread_mutex_init .

    $manpthread_mutex_init No manual entryfor pthread_mutex_init 解决方案: $sudo apt-get install manpages-po ...

  6. 解决UC手机字体变大的有关问题

    解决UC手机字体变大的问题 UC手机浏览器在识别到页面文字很多的情况下会自动放大字体优化阅读体验,如果要关闭这个功能需要在网页头部添加: <meta name="wap-font-sc ...

  7. HDU 4370 0 or 1(spfa+思维建图+计算最小环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...

  8. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  9. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  10. Remove Duplicates from Sorted Array I&&II——怎样防超时

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...