单调队列优化dp;

对于每个点开个单调队列,按转移到它的点到它的距离从大到小,得分也从大到小排列。

每次枚举当前点前面的所有点,对于每个点的队列中二分一个距离小于等于它到当前点的答案值,放到当前点的队列中。

//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
typedef long long LL;
using namespace std;
const int maxn=1005;
int n,que[maxn][maxn],f[maxn][maxn],ql[maxn],qr[maxn],ans=-1e9; namespace fastIO {
const int sz=1<<15|1;
char ch,buf[sz],*l,*r;
void gechar(char &c) {
if(l==r) r=(l=buf)+fread(buf,1,sz,stdin);
c = l==r?(char)EOF:*l++;
}
template<typename T> void read(T &x) {
int f=1; x=0; gechar(ch);
while(ch!='-'&&(ch<'0'||ch>'9')) gechar(ch);
if(ch=='-') f=-1,gechar(ch);
for(;ch>='0'&&ch<='9';gechar(ch)) x=x*10+ch-'0'; x*=f;
}
} struct node {
int pos,v;
friend bool operator <(const node &A,const node &B) {
return A.pos<B.pos;
}
}p[maxn]; int ef(int i,int lim) {
int l=ql[i],r=qr[i],res=-1;
while(l<=r) {
int mid=(l+r)>>1;
if(que[i][mid]<=lim) res=mid,r=mid-1;
else l=mid+1;
}
return res;
} void solve() {
for(int i=1;i<=n;i++) {
for(int j=1;j<=i-1;j++) {
int tp=ef(j,p[i].pos-p[j].pos);
if(tp!=-1) {
int pp=p[i].pos-p[j].pos,vv=f[j][tp]+p[i].v;
while(ql[i]<=qr[i]&&vv>=f[i][qr[i]]) qr[i]--;
que[i][++qr[i]]=pp;
f[i][qr[i]]=vv;
}
}
que[i][++qr[i]]=0;
f[i][qr[i]]=p[i].v;
ans=max(ans,f[i][1]);
}
} void init() {
fastIO::read(n);
for(int i=1;i<=n;i++) {
fastIO::read(p[i].pos);
fastIO::read(p[i].v);
}
} void work() {
sort(p+1,p+n+1);
for(int i=1;i<=n;i++) ql[i]=1,qr[i]=0;
solve();
for(int i=1;i<=n;i++) ql[i]=1,qr[i]=0;
for(int i=1;i<=n;i++) p[i].pos=1e6-p[i].pos;
sort(p+1,p+n+1);
solve();
printf("%d\n",ans);
} //#define DEBUG
int main() {
#ifdef DEBUG
freopen("1.in","r",stdin);
#endif
init();
work();
return 0;
}

  

洛谷 3089 [USACO13NOV]POGO的牛Pogo-Cow的更多相关文章

  1. 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】

    P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...

  2. HAOI2006 (洛谷P2341)受欢迎的牛 题解

    HAOI2006 (洛谷P2341)受欢迎的牛 题解 题目描述 友情链接原题 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之 ...

  3. 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S

    本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...

  4. 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解

    P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...

  5. 洛谷P2341受欢迎的牛

    传送门啦 这是一个tarjan强连通分量与出度结合的例题. 先明确一下题意,如果这个点(缩点之后的)没有出度,这个点才能成为明星牛(明星牛的定义是:所有牛都喜欢他才可以). 由于我们进行了缩点,所以我 ...

  6. 洛谷P3092 [USACO13NOV]没有找零No Change

    P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...

  7. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  8. 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)

    题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...

  9. 洛谷 P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

随机推荐

  1. 基本包装类型Boolean、Number、String特性及常用方法

    基本包装类型:Boolean.Number.String 一.String 字符串常用方法 1.indexOf()  lastIndexOf()  返回相应字符的索引号 2.slice(index1, ...

  2. Vim操作 -- 按列

    1, 拷贝行 Y y 拷贝当前光标字符 如果需要拷贝整个单词,可以用 ye,e表示跳到词尾 2, 粘贴 P(大写) 粘贴到光标前 p(小写)粘贴到光标后 3, 进入快操作模式 ctrl+q 4, 用 ...

  3. 安装Ubuntu16.04卡在logo界面

    问题背景 笔者在使用U盘UEFI模式安装Ubuntu16.04时,遇到一个问题,即在BIOS里的boot设置U盘为第一启动项之后,启动,并没有顺利进入系统,而是卡在了logo界面.(PS:其实我等了它 ...

  4. SpringData_01_SpringData的快速入门

    1.1SpringData JPA概述 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问 ...

  5. 软件工程(C编码实践篇)学习总结

    吴磊 SA17225400 学习完了孟老师的软件工程(C编码实践篇),跟着老师一步步的完成了代码的编写,收获真的很大. 在学习这门课之前,我还不会用Linux,也没接触过Git.如今我已成能够在Lin ...

  6. vue页面刷新数据丢失问题

    参考: https://blog.csdn.net/aliven1/article/details/80743470          https://blog.csdn.net/liang37712 ...

  7. Mybatis的插件 PageHelper 分页查询使用方法

    参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...

  8. mahout in Action2.2-给用户推荐图书(3)-评价推荐系统

    推荐系统引擎是一个工具,一种回答问题的手段,"对用户来讲什么是最好的推荐?",在研究回答的前先研究一下这个问题.一个好的推荐的准确含义是什么?如何知道推荐系统是如何生成推荐的?下面 ...

  9. gethostname gethostbyname gethostbyaddr 获得主机相关信息

    网络编程里经常需要获得主机的相关信息,下面围绕相关的函数以及用到的结构来说明. 获得主机名:int gethostname( char FAR *name, //[out] Pointer to a ...

  10. vue使用远程在线更新代码

    一.main.js import Vue from 'vue' import App from './App' import router from './router' import Vuex fr ...