vijos 1780 开车旅行
细节巨多。
倍增即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
#include<cstdlib>
#define maxv 200500
#define maxn 100500
#define eps 1e-7
#define inf 0x7f7f7f7f7f7f7f7fLL
using namespace std;
long long n,m,h[maxn],x,y,aft[maxn][],anc[maxv][],dis1[maxv][],dis2[maxv][],hashed[maxn],pos[maxn];
long long ret1=,ret2=,ans;
struct pnt
{
long long ret,dot,rets;
}p[];
set <long long> s;
set <long long> ::iterator it;
double ans1=inf;
bool cmp(pnt x,pnt y)
{
if (x.ret!=y.ret) return x.ret<y.ret;
return x.rets>y.rets;
}
void divide()
{
for (long long i=;i<=n;i++) hashed[i]=h[i];
sort(hashed+,hashed+n+);
for (long long i=;i<=n;i++)
{
h[i]=lower_bound(hashed+,hashed+n+,h[i])-hashed;
pos[h[i]]=i;
}
}
long long ask(long long x,long long y)
{
ret1=;ret2=;
for (long long e=;e>=;e--)
{
if ((ret1+ret2+dis1[x][e]+dis2[x][e]<=y) && (anc[x][e]))
{
ret1+=dis1[x][e];ret2+=dis2[x][e];
x=anc[x][e];
}
}
}
void update(long long x)
{
if (!ret1) return;
if ((double)ret2/ret1<ans1) {ans1=(double)ret2/ret1;ans=x;}
else if (((double)ret2/ret1-ans1<=eps) && (hashed[h[ans]]<hashed[h[x]])) ans=x;
}
int main()
{
scanf("%lld",&n);
for (long long i=;i<=n;i++) scanf("%lld",&h[i]);
divide();
for (long long i=n;i>=;i--)
{
for (long long j=;j<=;j++) p[j].dot=p[j].ret=inf;
it=s.lower_bound(h[i]);
if (it!=s.end())
{
p[].ret=hashed[h[pos[*it]]];p[].dot=pos[*it];
if (++it!=s.end()) {p[].ret=hashed[h[pos[*it]]];p[].dot=pos[*it];}
it--;
}
if (it!=s.begin())
{
it--;
p[].ret=hashed[h[pos[*it]]];p[].dot=pos[*it];
if (it!=s.begin()) {it--;p[].ret=hashed[h[pos[*it]]];p[].dot=pos[*it];it++;}
}
for (int j=;j<=;j++)
{
if (p[j].ret!=inf)
{
p[j].rets=hashed[h[i]]-p[j].ret;
p[j].ret=abs(hashed[h[i]]-p[j].ret);
}
}
sort(p+,p+,cmp);
if (p[].ret!=inf) aft[i][]=p[].dot;
if (p[].ret!=inf) aft[i][]=p[].dot;
s.insert(h[i]);
}
for (long long i=;i<=n;i++)
{
anc[*i-][]=*aft[i][];dis1[*i-][]=abs(hashed[h[aft[i][]]]-hashed[h[i]]);dis2[*i-][]=;
anc[*i][]=max(*aft[i][]-,(long long));dis1[*i][]=;dis2[*i][]=abs(hashed[h[aft[i][]]]-hashed[h[i]]);
}
n*=;
for (long long e=;e<=;e++)
for (long long i=;i<=n;i++)
{
anc[i][e]=anc[anc[i][e-]][e-];
dis1[i][e]=dis1[i][e-]+dis1[anc[i][e-]][e-];
dis2[i][e]=dis2[i][e-]+dis2[anc[i][e-]][e-];
}
scanf("%lld",&x);
for (long long i=;i<=n;i+=)
{ask(i,x);update(i/);}
printf("%d\n",ans);
scanf("%lld",&m);
for (long long i=;i<=m;i++)
{
scanf("%lld%lld",&x,&y);
ask(*x,y);
printf("%lld %lld\n",ret2,ret1);
}
return ;
}
vijos 1780 开车旅行的更多相关文章
- [NOIP2012提高组]开车旅行
题目:洛谷P1081.Vijos P1780.codevs1199. 题目大意:有n座海拔高度不相同的城市(编号1~n),两城市的距离就是两城市海拔之差.规定每次只能从编号小的城市走到编号大的城市. ...
- Luogu 1081 【NOIP2012】开车旅行 (链表,倍增)
Luogu 1081 [NOIP2012]开车旅行 (链表,倍增) Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已 ...
- CH5701 开车旅行
题意 5701 开车旅行 0x50「动态规划」例题 描述 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...
- 2012Noip提高组Day1 T3 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- Cogs 1264. [NOIP2012] 开车旅行(70分 暴力)
1264. [NOIP2012] 开车旅行 ★★☆ 输入文件:drive.in 输出文件:drive.out 简单对比时间限制:2 s 内存限制:128 MB [题目描述] 小A 和小 ...
- 开车旅行 【NOIP2012 D1T3】
开车旅行 [NOIP2012 D1T3] 倍增 首先令\(a[i]\)表示从i出发最近的城市下标,\(b[i]\)表示从i出发第二近的城市下标 可以维护一个\(\text{set<pair< ...
- 洛谷 P1081 开车旅行(70)
P1081 开车旅行 题目描述 小AA 和小BB 决定利用假期外出旅行,他们将想去的城市从 11到 NN 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ii的海 ...
- 开车旅行 2012年NOIP全国联赛提高组(倍增+set)
开车旅行 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小A 和小B决定利用 ...
- P1081 [NOIP2012]开车旅行[倍增]
P1081 开车旅行 题面较为啰嗦.大概概括:一个数列,只能从一个点向后走,两种方案:A.走到和自己差的绝对值次小的点B.走到和自己差的绝对值最小点:花费为此差绝对值:若干询问从规定点向后最多花 ...
随机推荐
- 笔记8:winfrom连接数据库DBHelp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 221. Maximal Square -- 矩阵中1组成的最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- JSP 服务器响应
Response响应对象主要将JSP容器处理后的结果传回到客户端.可以通过response变量设置HTTP的状态和向客户端发送数据,如Cookie.HTTP文件头信息等. 一个典型的响应看起来就像下面 ...
- 再谈IT行业工程师文化
为什么是再呢?因为“工程师文化”这种说法网上很多,各种理解,各种版 本,我只是简单说说我的认识,说的不对的地方敬请各位大牛,高手,高高手多多指教,我本身也是个技术人员,不过只是技术不怎么样而已.写这个 ...
- ZOJ 3652 Maze 模拟,bfs,读题 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842 要注意题目中两点: 1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才 ...
- Java中断言的使用(转)
相信学过c,c++的朋友一定不会对断言感到陌生.下面介绍一下断言在JAVA中的使用,JAVA是从JDK1.4才开始支持断言的(添加了关键字assert),请注意老版的JRE不支持. 断言概述 编写代码 ...
- IIS CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问
解决方案:给Windows下temp文件添IIS_USERS权限即可
- Echarts 地图控件tooltip多行显示
直接上代码 var o = { "tooltip": { trigger: 'item', "formatter": function (params) { v ...
- 【HDU2087】KMP
KMP算法其实很好理解,就是在匹配串中找最近的相同的串. 下面是HDU的2087: #include<iostream> #include<cstdio> #include&l ...