有显然的 \(O(n^3)\) 做法,可以获得 \(38pts\)。(退火在洛谷上能跑 \(75pts\))

答案具有单调性,考虑二分一个 \(M\) 并判断。列出 \(i\) 到 \(j\) 的距离公式:

  1. 求出 \(l_i\) 的前缀和数组 \(L\),不加边的情况,\(i\) 和 \(j\) 的距离就是 \(L_j-L_i+d_i+d_j\)。

  2. 在 \(a,b\) 之间加入一条长度为 \(C\) 的边,距离和 \(|L_i-L_a|+|L_j-L_b|+d_i+d_j+C\) 取 \(\min\)。

注意到,在公式 \(1\) 中,如果值已经 \(\le M\) 的可以不用管。需要计算的是 \(1\) 中 \(\gt M\) 的 \((i,j)\) 中 \(2\) 式的最大值。

绝对值很不好看,将其暴力拆开:

\[L_i+L_j + d_i+d_j+C-M \le L_a+L_b \quad (L_i \ge L_a, L_j \ge L_b) \tag{1}
\]
\[L_i-L_j + d_i+d_j+C-M \le L_a-L_b \quad (L_i \ge L_a, L_j \lt L_b) \tag{2}
\]
\[L_i-L_j - d_i-d_j-C+M \ge L_a-L_b \quad (L_i \lt L_a, L_j \ge L_b) \tag{3}
\]
\[L_i+L_j - d_i-d_j-C+M \ge L_a+L_b \quad (L_i \lt L_a, L_j \lt L_b) \tag{4}
\]

对于公式 \((1)\),显然若括号内条件不成立,求出的答案一定会更小,无法更新最大值。

对于公式 \((2)(3)(4)\) 也是同理,若括号内条件不成立则不会对不等式的解集产生贡献。

所以,可以不考虑括号内的条件,分别求出上述 \(4\) 个不等式左边的最大/最小值,并考察是否有 \((a,b)\) 满足其不等式。

至于不等式左侧的最值,可以很方便地枚举 \(j\) 并使用单调队列求出最优的 \(i\)。求出左侧的 \(4\) 个最值后,直接枚举 \(b\),并维护 \(a\) 的两个可行区间,判断是否有交。

结合最外层的二分,本题时间复杂度为 \(O(n \log n)\),可以通过全部数据。

/**
* @file: shortcut.cpp
* @author: yaoxi-std
* @url:
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar((x % 10) ^ 48);
}
bool m_be;
using ll = long long;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int n, cst, que[MAXN];
ll len[MAXN], dis[MAXN];
bool check(ll mid) {
int fr = 1, bk = 0;
// vmin: min{len[i]-dis[i]}, vmax: max{len[i]+dis[i]}
ll vmin = 1e18, vmax = -1e18;
// mx1 ~ mn4 对应 4 个式子
ll mx1 = -1e18, mx2 = -1e18, mn3 = 1e18, mn4 = 1e18;
for (int i = 1; i <= n; ++i) {
if (len[i] + dis[i] - vmin > mid) {
while (fr <= bk && len[i] - len[que[fr]] + dis[que[fr]] + dis[i] > mid)
vmax = max(vmax, len[que[fr]] + dis[que[fr]]), ++fr;
mx1 = max(mx1, vmax + len[i] + dis[i] + cst - mid);
mx2 = max(mx2, vmax - len[i] + dis[i] + cst - mid);
mn3 = min(mn3, vmin - len[i] - dis[i] - cst + mid);
mn4 = min(mn4, vmin + len[i] - dis[i] - cst + mid);
}
vmin = min(vmin, len[i] - dis[i]);
while (fr <= bk && dis[que[bk]] - len[que[bk]] <= dis[i] - len[i]) --bk;
que[++bk] = i;
}
if (mn4 < mx1 || mn3 < mx2) return false;
int l1 = n + 1, r1 = n, l2 = 1, r2 = 0;
for (int i = 1; i <= n; ++i) {
while (l1 > 1 && len[i] + len[l1 - 1] >= mx1) --l1;
while (l1 <= r1 && len[i] + len[r1] > mn4) --r1;
while (r2 < n && len[r2 + 1] - len[i] <= mn3) ++r2;
while (l2 <= r2 && len[l2] - len[i] < mx2) ++l2;
if (l1 > r1 || l2 > r2) continue;
if (max(l1, l2) <= min(r1, r2)) return true;
}
return false;
}
bool m_ed;
signed main() {
// resetIO(shortcut);
// debug("Mem %.5lfMB.", fabs(&m_ed - &m_be) / 1048576);
read(n), read(cst);
for (int i = 2; i <= n; ++i) read(len[i]);
for (int i = 1; i <= n; ++i) read(dis[i]);
for (int i = 2; i <= n; ++i) len[i] += len[i - 1];
ll l = 0, r = 1e18, ans = 1e18;
while (l <= r) {
ll mid = (l + r) >> 1;
if (check(mid)) {
r = mid - 1, ans = mid;
} else {
l = mid + 1;
}
}
write(ans), putchar('\n');
return 0;
}

[IOI2016] shortcut的更多相关文章

  1. Eclipse CDT: Shortcut to switch between .h and .cpp

    ctrl+ tab  is the default shortcut.You can change it in Window → Preferences → General → Keys: Toggl ...

  2. android shortcut &livefoulder

    android shortcut(实现步骤) 1.建立activity 2.minifest 里面注册并加上intent-filter,name为:android.intent.action.CREA ...

  3. 关于favicon.ico,shortcut icon,icon

    引入一篇文章.关于favicon.ico二三事. http://www.cnblogs.com/LoveJenny/archive/2012/05/22/2512683.html 一直对favicon ...

  4. Linux Shell shortcut

    Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置 Details see below: Linux shell shortcut

  5. [转] Fix: Screen Clipping Shortcut In OneNote Not Working After Upgrading To Windows 8.1

    RECOMMENDED: Click here to fix Windows errors and optimize system performance No doubt, OneNote is y ...

  6. Xcode Custom Shortcut

    edit file "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources" add < ...

  7. Android添加快捷方式(Shortcut)到手机桌面

    Android添加快捷方式(Short)到手机桌面 权限 要在手机桌面上添加快捷方式,首先需要在manifest中添加权限. <!-- 添加快捷方式 --> <uses-permis ...

  8. Django views 中的 shortcut function

    shortcut function都在django.shortcuts这个包中,主要包含有:render(), render_to_response(), redirect(), get_object ...

  9. apple-touch-icon,shortcut icon和icon的区别(手机站发送到手机桌面图标自定义)

    apple-touch-icon 可以了解到这是一个类似网站favicon的图标文件,用来在iphone和ipod上创建快捷键时使用. 这个文件应当是png格式,57x57像素大小,放在网站根目录之下 ...

随机推荐

  1. 设计一个网上书店,该系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,所有的语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元

    现使用策略模式来设计该系统,绘制类图并编程实现 UML类图 书籍 package com.zheng; public class Book { private double price;// 价格 p ...

  2. 15行python代码实现人脸识别

    方法一:face_recognition import cv2 import face_recognition img_path = "C:/Users/CJK/Desktop/1.jpg& ...

  3. 知识图谱顶会论文(ACL-2022) PKGC:预训练模型是否有利于KGC?可靠的评估和合理的方法

    PKGC:预训练模型是否有利于KGC?可靠的评估和合理的方法 论文地址:Do Pre-trained Models Benefit Knowledge Graph Completion? A Reli ...

  4. 4.websocket基本概念

    websockey的模式就是在于当前端向后端发送请求创建一个websocket链连接之后,连接默认不断开,前端和服务端就维护了一个连接,前端可以通过连接给服务端发消息,服务端也可以通过连接给前端发消息 ...

  5. Linux软件安装方式 - Tarball&RPM&YUM

    软件安装 简介 概念详解 # 概念详解 - 开放源码: 程序码, 写给人类看的程序语言, 但机器并不认识, 所以无法执行; - 编译器: 将程序码转译成为机器看的懂得语言, 就类似翻译者的角色; - ...

  6. nrf9160 做modem—— 连接云(接入方式MQTT)

    今天测试把nrf9160作为modem的例程Serial LTE Modem程序(后面简称slm),何为做modem,通俗来说就是将nrf9160作为无线模块,主控由其余MCU做,主控通过AT命令控制 ...

  7. 关于网页实现串口或者TCP通讯的说明

    概述 最近经常有网页联系我,反馈为什么他按我说的方法,写的HTML代码,无法在chrome网页中运行.这里我统一做一个解释,我发现好多网页并没有理解我的意思. 其实,要实现在HTML中进行串口或者TC ...

  8. go基础语法50问,来看看你的go基础合格了吗?

    目录 1.使用值为 nil 的 slice.map会发生啥 2.访问 map 中的 key,需要注意啥 3.string 类型的值可以修改吗 4.switch 中如何强制执行下一个 case 代码块 ...

  9. Java 超新星开源项目 Solon v1.10.10 发布

    一个更现代感的 Java 应用开发框架:更快.更小.更自由.主框架仅 0.1 MB.Helloworld: @Controller public class App { public static v ...

  10. Oracle数据库允许最大连接数

    1.查看当前的数据库连接数 SQL> select count(*) from v$process ; 2.数据库允许的最大连接数 SQL> select value from v$par ...