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. [cocos2d-x]深入--几个代表性的类 (续)

    摘要: 此文对cocos2d-x引擎中最具代表性,最能体现框架结构的几个类做了简单的介绍, 包括Director,Application, Renderer, EventDispatcher, Sch ...

  2. Object-C中ARC forbids explicit message send of ' ' 错误

    OC中ARC forbids explicit message send of '...'错误 转自CSDN hahahacff 有所整理 ARC forbids explicit message s ...

  3. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  4. qau-国庆七天乐——B

      B - Bull Math   Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Sub ...

  5. Window 对象

    Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(<frame> 或 <iframe> 标签),浏览器会为 HTML 文档创建一个 window ...

  6. Android手机浏览器访问本地网络相关问题

    为了测试开发的手机网站,常常需要使手机直接访问本地网络. 在这个过程中碰到几个问题,记下来供以后参考 1. 在本地主机运行apache后,使用localhost和127.0.0.1可以访问页面,但使用 ...

  7. ASP.NET MVC铵钮Click后下载文件

    本篇Insus.NET练习的是FilePathResult和FileStreamResult操作.本篇也算是<如何把Json格式字符写进text文件中>http://www.cnblogs ...

  8. Working Set缓存算法(转)

    为了加深对缓存算法的理解,特转此篇,又由于本文内容过多,故不做翻译,原文地址Working Set页面置换算法 In the purest form of paging, processes are ...

  9. java中String、StringBuffer、StringBuilder的区别

    java中String.StringBuffer.StringBuilder是编程中经常使用的字符串类,他们之间的区别也是经常在面试中会问到的问题.现在总结一下,看看他们的不同与相同. 1.可变与不可 ...

  10. c++ 指针(一)

    指针:是说指针名表示的是地址.是一个变量,存储的是值的地址,而不是值本身 *运算符被称为间接值或解除引用运算符也可以叫做取地址符 声明一个指针 int * p_data; * p_data的类型为in ...