洛谷 3089 [USACO13NOV]POGO的牛Pogo-Cow
单调队列优化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的更多相关文章
- 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】
P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...
- HAOI2006 (洛谷P2341)受欢迎的牛 题解
HAOI2006 (洛谷P2341)受欢迎的牛 题解 题目描述 友情链接原题 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之 ...
- 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...
- 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...
- 洛谷P2341受欢迎的牛
传送门啦 这是一个tarjan强连通分量与出度结合的例题. 先明确一下题意,如果这个点(缩点之后的)没有出度,这个点才能成为明星牛(明星牛的定义是:所有牛都喜欢他才可以). 由于我们进行了缩点,所以我 ...
- 洛谷P3092 [USACO13NOV]没有找零No Change
P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)
题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...
- 洛谷 P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
随机推荐
- 用 Docker 搭建Sha--dow--sock--s 笔记
首先得找一台海外服务器,该服务器一定要在海外. 一.在https://my.vultr.com/购买一台海外服务器,亲测选美国Miami速度最稳: 二.系统我选了CentOS 7 x64,在CentO ...
- 动软DbHelperSQL
using System; using System.Collections; using System.Data; using System.Data.SqlClient; using System ...
- CentOS7服务器中apache、php7以及mysql5.7的安装配置代码
CentOS7服务器中apache.php7以及mysql5.7的配置代码如下所示: yum upgradeyum install net-tools 安装apache (http://m.86822 ...
- Ubuntu16.04安装pcl库
sudo apt-get install libpcl-dev sudo apt-get install pcl-tools
- python Xls文档读写
1.模块安装 2.python 代码 import xlrd import xlwt import datetime def set_style(name,height,format,bold=Fal ...
- LUOGU P4163 [SCOI2007]排列
传送门 解题思路 首先我们发现这道题s的长度很小,所以考虑点暴力的做法,状压dp或搜索.本蒟蒻搜索永远调不对,所以就写了个状压dp.因为所有s里的数都要出现一次,并且最后的答案是要求整除,那么我们设d ...
- 一台电脑同时添加git和bitbucket两个网站的ssh key
添加第一个ssh key 就不多说了,不懂的可以自己查资料 ssh-keygen -t rsa -C 'email_1@email.com' 然后一路enter就好了 假设已经添加好了git的ssh ...
- js 实现页面局部(或图片)放大功能(vue)
方法: adjustStart1 (e) { e.preventDefault() let event = e.touches if (event.length === 2) { this.style ...
- 使用HTTP代理
HTTP代理服务器可以比作客户端与Web服务器网站之间的一个信息中转站,客户端发送的HTTP请求和Web服务器返回的HTTP响应通过代理服务器转发给对方, 爬虫程序在爬取某些网站的时候也需要使用代理, ...
- ES6之主要知识点(五)函数
函数参数的默认值 作用域 ; function f(x, y = x) { console.log(y); } f() let x = ; function f(y = x) { let x = ; ...