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. 《Invert》开发日志02:游戏风格定型

    声明:以下涉及到的<God of Light>.<Valiant Hearts : The Great War>.<Angry Birds 2>游戏截图均来自其Ap ...

  2. HttpURLConnection用法详解

    针对JDK中的URLConnection连接Servlet的问题,网上有虽然有所涉及,但是只是说明了某一个或几个问题,是以FAQ的方式来解决的,而且比较零散,现在对这个类的使用就本人在项目中的使用经验 ...

  3. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. UWP数据绑定

    [ 已针对 Windows 10 上的 UWP 应用更新. 有关 Windows 8.x 文章,请参阅存档 ] 数据绑定是你的应用 UI 用来显示数据的一种方法,可以选择与该数据保持同步. 借助数据绑 ...

  5. 一行代码远离Google浏览器兼容问题的困扰

    跟Html相比,Web Flash开发的最大优势就在于兼容性好,因为FlashPlayer的开发商只有Adobe一家.但自从Google插了一脚进来,改版出自己的FlashPlayer以后,这一优势就 ...

  6. date时间函数

    时间函数: date();和time();的相互转换 time();   在PHP中单位是秒,在js中是毫秒. microtime();  毫秒 date('Y-m-d H:i:s',time()); ...

  7. poj2580 Super Memmo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  8. Android应用程序(APK)的编译打包过程

    (9878) (7) 现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的 ...

  9. webpack.optimize.CommonsChunkPlugin插件的使用

    方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin('common.js'), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...

  10. BZOJ 1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14396  Solved: 5521[Submit][Sta ...