P1081 开车旅行
这题花了好长时间去调试(蔡),最后在cxk鸡你太美的歌声中发现了问题
我花了一个NOIPD1时间来写这个noipD1T3...
题目大意:给定n个城市排成一排,只能从1~n的方向走,定义两个城市的距离是高度差。两个人开一辆车去旅行,A先开选次近的点,B选最近的点,两人交替开车,这里距离的定义是:如果距离相等,选高度小的更近。求:1)从任意城市出发,给定x(走的最远的距离),A开的路程/B开的路程最小是多少,B的路程为0是认为是无穷大,且无穷大认为相等,如果有多个城市满足条件,输出海拔最高的;2)m组询问,每次询问给定s(起点)和x(走的最远的距离),尽量远走求A和B走的距离分别是多少。
首先看数据,似乎可以暴力得个部分分,每次选取一个点就开始模拟行走.每次都要寻找第一近和第二近的点,非常浪费时间,那么就预处理。
怎么预处理呢?排序吗?有点复杂,离得最近的一定是高度差相近的,那么能排序并且在相近的元素中寻找的数据结构是什么呢?STL的set!每次找两个前驱和后继,然后排序取前两个.不过要注意,插入set要倒着插,因为每次都是去东边的城市.但是这样预处理之后还是不能通过所有的数据,我们来对算法仔细的分析,查找第一近和第二近的优化过了,枚举点似乎不能优化,那么只能优化模拟行走了.会发现两次行走可以合并成一次。
有没有什么更快的枚举技巧呢?就是倍增了.设f[i][j]为在位置i走2^j轮的位置,一轮就是A走和B走一次,fa[i][j]就是A在位置i走2^j轮的距离,fb[i][j]类同,那么怎么算呢?记住一个重要的性质:a^i-1 + a^i-1 = a^i,递推也可以这样,f[i][j] = f[f[i][j-1]][j-1],fa[i][j] = fa[i][j-1] + fa[f[i][j-1]][j-1],fb类同,因为n<=100000,所以2^i,i从19开始枚举,从大到小枚举,依次判断行不行即可.(真是恶心......)
代码:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <set> using namespace std; const int maxn = 100005; int n, x0, m, k,x1, na[maxn], nb[maxn],ans; long long ansa = 1e5,ansb = 0ll,fa[maxn][20], fb[maxn][20], f[maxn][20]; struct node { int id, high; bool operator < (const node & b) const { return high < b.high; } }c[maxn]; struct node2 { int id, gaoducha; bool operator < (const node2 & b) const { if (gaoducha != b.gaoducha) return gaoducha < b.gaoducha; else return c[id].high < c[b.id].high; } }temp[5]; set <node> s; void init(int i) { set <node> ::iterator it = s.find(c[i]); int j = 0; if (it != s.begin()) { --it; temp[++j] = (node2) { it->id, abs(it->high - c[i].high) }; if (it != s.begin()) { --it; temp[++j] = (node2) { it->id, abs(it->high - c[i].high) }; ++it; } ++it; } if ((++it) != s.end())//s.end()是结尾之后的! { temp[++j] = (node2) { it->id, abs(it->high - c[i].high) }; if ((++it) != s.end()) temp[++j] = (node2) { it->id, abs(it->high - c[i].high) }; } sort(temp + 1, temp + j + 1); nb[i] = temp[1].id; if (j == 1) return; na[i] = temp[2].id; return; } void query(int st, int x, long long &ta, long long &tb) { for (int i = 19; i >= 0; i--) if (f[st][i] && fa[st][i] + fb[st][i] <= x) { ta += fa[st][i]; tb += fb[st][i]; x -= fa[st][i] + fb[st][i]; st = f[st][i]; } if (!na[st]) return; int tempans = abs(c[na[st]].high - c[st].high); if (tempans <= x) ta += tempans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &c[i].high); c[i].id = i; } for (int i = n; i;i--) { s.insert(c[i]); if (i != n) init(i); } for (int i = 1; i <= n; i++) { int p1 = na[i], p2 = nb[na[i]]; fa[i][0] = p1 ? abs(c[p1].high - c[i].high) : 0; fb[i][0] = p2 ? abs(c[p2].high - c[p1].high) : 0; f[i][0] = p2; } for (int j = 1; j < 20;j++) //后更新的是j所以先枚举j for (int i = 1; i <= n; i++) { f[i][j] = f[f[i][j - 1]][j - 1]; fa[i][j] = fa[i][j - 1] + fa[f[i][j - 1]][j - 1]; fb[i][j] = fb[i][j - 1] + fb[f[i][j - 1]][j - 1]; } scanf("%d", &x0); ans = 0; for (int i = 1; i <= n; i++) { long long ta = 0ll, tb = 0ll; query(i, x0, ta, tb); if (tb && (!ans || ansa * tb > ansb * ta)) { ansa = ta; ansb = tb; ans = i; } } printf("%d ", ans); scanf("%d", &m); while (m--) { scanf("%d%d", &k, &x1); long long ta = 0ll, tb = 0ll; query(k, x1, ta, tb); printf("%d %d ", ta, tb); } return 0; }
2019-08-01 22:14:03