http://codevs.cn/problem/1199/

主要思想是倍增,对于第一个回答从后往前扫,依次插入平衡树中。

我写的splay,比较繁琐。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100003;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
} int h[N], n, m, nxt_a[N], nxt_b[N], f[N][18], sum_a[N][18], sum_b[N][18]; struct Splay {
struct node {
node *ch[2], *fa;
int id, num;
bool pl() {return fa->ch[1] == this;}
void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
} *root, *null;
void init() {
null = new node;
null->id = null->num = 0;
null->ch[0] = null->ch[1] = null->fa = null;
root = null;
}
void printall(node *r) {
if (r == null) return;
printall(r->ch[0]);
printf("%d ", r->num);
printall(r->ch[1]);
}
void rotate(node *r) {
node *f = r->fa;
bool c = r->pl();
if (f != root) f->fa->setc(r, f->pl());
else root = r, r->fa = null;
f->setc(r->ch[!c], c);
r->setc(f, !c);
}
void splay(node *r) {
for(; r->fa != null; rotate(r))
if (r->fa->fa != null) rotate(r->fa->pl() == r->pl() ? r->fa : r);
}
struct data {
int del, h, id;
bool operator < (const data &A) const {
return (del == A.del ? h < A.h : del < A.del);
}
} a[5];
node *getl(int num, int to) {
node *r = root->ch[0];
if (r == null) {a[to] = (data) {0x7fffffff, 0, 0}; return r;}
while (r->ch[1] != null) r = r->ch[1];
a[to] = (data) {abs(num - r->num), r->num, r->id};
return r;
}
node *getr(int num, int to) {
node *r = root->ch[1];
if (r == null) {a[to] = (data) {0x7fffffff, 0, 0}; return r;}
while (r->ch[0] != null) r = r->ch[0];
a[to] = (data) {abs(num - r->num), r->num, r->id};
return r;
}
void mk_nxt(int &back_1, int &back_2, int id, int num) {
node *r = root;
if (r == null) {
root = new node;
root->id = id; root->num = num;
root->ch[0] = root->ch[1] = root->fa = null;
back_1 = back_2 = 0;
return;
}
bool c;
while (true) {
if (r->num > num) c = 0;
else c = 1;
if (r->ch[c] == null) {
r->ch[c] = new node;
r->ch[c]->fa = r;
r = r->ch[c];
r->id = id; r->num = num;
r->ch[0] = r->ch[1] = null;
splay(r);
node *ll = getl(num, 0), *rr = getr(num, 1);
if (ll != null) {splay(ll); getl(num, 2);} else a[2] = (data) {0x7fffffff, 0, 0};
if (rr != null) {splay(rr); getr(num, 3);} else a[3] = (data) {0x7fffffff, 0, 0};
sort(a, a + 4);
if (a[1].id != 0) {
back_1 = a[1].id; back_2 = a[0].id;
} else if (a[0].id != 0) {
back_1 = 0; back_2 = a[0].id;
} else {
back_1 = 0; back_2 = 0;
}
return;
} else r = r->ch[c];
}
}
} T; void cal(int &a, int &b, int s, int x) {
a = b = 0;
for(int i = 17; i >= 0; --i)
if (f[s][i] && sum_a[s][i] + sum_b[s][i] <= x) {
a += sum_a[s][i]; b += sum_b[s][i];
x -= sum_a[s][i]; x -= sum_b[s][i];
s = f[s][i];
}
if (sum_a[s][0] <= x && nxt_a[s])
a += sum_a[s][0];
} int main() {
T.init();
n = in();
for(int i = 1; i <= n; ++i) h[i] = in();
for(int i = n; i >= 1; --i) T.mk_nxt(nxt_a[i], nxt_b[i], i, h[i]); for(int i = 1; i <= n; ++i) {
if (nxt_a[i]) sum_a[i][0] = abs(h[nxt_a[i]] - h[i]);
if (nxt_b[nxt_a[i]]) {
sum_b[i][0] = abs(h[nxt_b[nxt_a[i]]] - h[nxt_a[i]]);
f[i][0] = nxt_b[nxt_a[i]];
}
}
for(int j = 1; j <= 17; ++j)
for(int i = 1; i <= n; ++i)
if (f[f[i][j - 1]][j - 1]) {
f[i][j] = f[f[i][j - 1]][j - 1];
sum_a[i][j] = sum_a[i][j - 1] + sum_a[f[i][j - 1]][j - 1];
sum_b[i][j] = sum_b[i][j - 1] + sum_b[f[i][j - 1]][j - 1];
} int a, b, s, x, ans = 0; double ans_num = -1.0, now;
x = in();
for(int i = 1; i <= n; ++i) {
cal(a, b, i, x);
if (b != 0) now = 1.0 * a / b;
else now = -1.0;
if (ans_num == -1.0 || (ans_num != -1.0 && now != -1.0 && now <= ans_num))
if (fabs(ans_num - now) < 1e-12) {
if (h[i] > h[ans]) ans = i;
} else ans_num = now, ans = i;
}
printf("%d\n", ans); m = in(); int i = 0;
while (m--) {
++i;
s = in(); x = in();
cal(a, b, s, x);
printf("%d %d\n", a, b);
} return 0;
}

_(:з」∠)_

【CodeVS 1199】【NOIP 2012】开车旅行的更多相关文章

  1. 【NOIP 2012 开车旅行】***

    题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...

  2. noip 2012 开车旅行

    /*考场上写的暴力 40分钟70分*/ #include<iostream> #include<cstdio> #include<cstring> #define ...

  3. 开车旅行 2012年NOIP全国联赛提高组(倍增+set)

    开车旅行 2012年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 小A 和小B决定利用 ...

  4. vijos P1780 【NOIP2012】 开车旅行

    描述 小\(A\)和小\(B\)决定利用假期外出旅行,他们将想去的城市从\(1\)到\(N\)编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市\(i\)的海拔高度为 ...

  5. [NOIP2012] 提高组 洛谷P1081 开车旅行

    题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...

  6. P1081 开车旅行

    题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...

  7. [NOIP2012T3]开车旅行

    题目描述 NOIP 2012 提高组 题3小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...

  8. NOIP 2012 Day2T2 借教室题解

    NOIP 2012 Day2T2 借教室题解 题目传送门:http://codevs.cn/problem/1217/ 题目描述 Description 在大学期间,经常需要租借教室.大到院系举办活动 ...

  9. NOIP 2012

    Prob.1 vigenere密码 模拟代码: #include<cstdio> #include<cstring> #include<iostream> usin ...

  10. NOIP 2012 Day1

    tags: NOIP 模拟 倍增 高精 Python categories: 信息学竞赛 总结 Luogu P1079 Vigenère 密码 Solution 表示并不是很懂其他人发的题解. 我是这 ...

随机推荐

  1. 洛谷U2641 木板面积(area)——S.B.S.

    题目背景 一年一次的夏令营又要开始了,卡卡西和小伙伴们早就做好了准备,满心期 待着这趟快乐之旅.在一个阳光明媚的清晨,卡卡西在老师的带领下来到了这次 夏令营的首站——“神奇木材加工厂” . 题目描述 ...

  2. 第9章 用内核对象进行线程同步(4)_死锁(DeadLock)及其他

    9.7 线程同步对象速查表 对象 何时处于未触发状态 何时处于触发状态 成功等待的副作用 进程 进程仍在运行的时候 进程终止的时(ExitProcess.TerminateProcess) 没有 线程 ...

  3. 这段时间对c#和java的感受

    这段时间对c#和java的感受 虽然很多书上说语法相似,但实际这是一个接近于门外汉的看法 真正的不同是 c#对更贴近系统API,      而java倡导跨平台 因而c#语法关键字更多,更细, 而ja ...

  4. Java MyEclipse下Ant build.xml简单实例详解

    一.下载配置ant 1.首先下载ant: http://www.apache.org/ 下载最新的版本2.解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:A ...

  5. javascript的几个小技巧

    1.在循环中缓存array.length 这个技巧很简单,这个在处理一个很大的数组循环时,对性能影响将是非常大的.基本上,大家都会写一个这样的同步迭代的数组. for(var i=0;i<arr ...

  6. python-数据类型补充及文件处理操作

    ___数据类型____ 一.列表的复制 浅复制和深复制 浅复制只复制一层,深复制完全克隆,慎用 1.实现浅复制的三种方式: name=['song','xiao','nan'] import copy ...

  7. avalon.js路由

    之前自己写了一个AJAX加载页面的方法:有时候一个页面里面会分区域加载不同的东西(div,html),但是IE的回退按钮,就失去任何意义了: 这两天研究了一下avalon.js的路由: 需要准备: 1 ...

  8. .Net Core 控制台输出中文乱码

    Net Core 控制台输出中文乱码的解决方法: public static void Main(string[] args)         {             Console.Output ...

  9. php加载xml编码错误,“Error: Input is not proper UTF-8, indicate encoding! ”

    最近在给php中解析xml的时候,抛出一个错误: "Warning: DOMDocument::load(): Input is not proper UTF-8, indicate enc ...

  10. linux可靠信号和非可靠信号测试样例

    不可靠信号(在执行自定义函数其间会丢失同类信号) 可靠信号(在执行自定义函数其间不会丢失同类信号) 不可靠信号用一次以后,就恢复其默认处理吗? 至少在ubuntu 12.04上,已经是一次绑定,永远使 ...