单调队列优化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. 记录一次工作中配置的Mysql主从复制过程

    Mysql主从复制教程 1.安装mysql(安装步骤跳过)2.配置密码.(如果忘记密码或者误操作删除了root用户,使用如下命令,没有忘记就跳到3)将skip-grant-tables放在/etc/m ...

  2. OpenGL键盘交互响应事件

    GLUT允许我们编写程序,在里面加入键盘输入控制,包括了普通键,和其他特殊键(如F1,UP).在这一章里我们将学习如何去检测哪个键被按下,可以从GLUT里得到些什么信息,和如何处理键盘输入.   处理 ...

  3. 网站PC端和移动端,用户通过设备识别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!--<me ...

  4. PostgreSQL:COALESCE函数

    COALESCE函数是返回参数中的第一个非null的值,它要求参数中至少有一个是非null的,如果参数都是null会报错. select COALESCE(null,null); //报错 selec ...

  5. leetcode-03-二叉树的锯齿层次遍历

    题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.va ...

  6. 【9.14NOIP模拟pj】wtaxi 题解——搜索

    [9.14NOIP模拟pj]wtaxi 题目简化 有K辆车,N个人,上车给D元,只有S分钟.上车后无论多少人都要给D元,原地等多少分钟就没了多少元.求最小花费的钱. 我的思路 毫无疑问,此题可以用搜索 ...

  7. ThinkPHP支持模型的分层

    ThinkPHP支持模型的分层 ,除了Model层之外,我们可以项目的需要设计和创建其他的模型层. 大理石平台支架 通常情况下,不同的分层模型仍然是继承系统的\Think\Model类或其子类,所以, ...

  8. 深入浅出 Java Concurrency (33): 线程池 part 6 线程池的实现及原理 (1)[转]

    线程池数据结构与线程构造方法 由于已经看到了ThreadPoolExecutor的源码,因此很容易就看到了ThreadPoolExecutor线程池的数据结构.图1描述了这种数据结构. 图1 Thre ...

  9. 机器学习(二)数据处理&相似/异性度量

    机器学习(二)数据处理&相似/异性度量 https://woaielf.github.io/2017/03/17/dm-2/ 2017-03-17 ZOE    数据科学  机器学习/数据挖掘 ...

  10. struts2的default.properties详解

    Struts 2框架有两个核心配置文件:struts.xml和struts.properties 其中struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result ...