跳到正文

题解

题解:P17201 DKM OI Round 1 - 廊途行

#SUSPENDED#约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月5日
收录
2026年10月4日
更新
2026年11月16日
篇幅
约 8 分钟
题目
P17201

版本

  1. v1当前

## 不想死就快跑!

题意简述

我们从房间 11 跑到房间 nn,有两种移动方式:

  1. 行走:i→i+1i \to i+1,代价 cc。
  2. 冲刺:i→j (j≥i+2)i \to j\ (j\ge i+2),代价 (j−i)2(j-i)^2。

特殊规则:

  • 冲刺之后如果还要继续移动,下一次必须行走;
  • 若冲刺是最后一步,则不需要后续行走;
  • 最多冲刺 kk 次。

求从 11 到达 nn 的最小总代价。

数据范围:1≤n≤10121\le n \le 10^{12},0≤k,c≤10120\le k,c \le 10^{12},1≤t≤1051\le t \le 10^5,答案会爆 long long。

分析

nn 最大到 101210^{12},DP 完全不可行,只能数学推导。

记总需要前进距离 L=n−1L=n-1。

如果使用 xx 次冲刺,最优策略一定让最后一次动作为冲刺,避免多付出一次强制行走的代价。

  • 前 x−1x-1 次冲刺:每次至少跳 22 格,后面强制走 11 格,单次合计最少消耗 33。
  • 最后一次冲刺:只需要跳至少 22 格,不需要后面走路。

xx 次冲刺最少占用总长度:

3(x−1)+2=3x−13(x-1)+2 = 3x-1

因此 3x−1≤L3x-1 \le L,得到 xx 的上界:

xmax=min⁡(k,⌊L+13⌋)x_{max}=\min\left(k,\left\lfloor \dfrac{L+1}{3}\right\rfloor\right)

设每一次冲刺跳跃长度为 a1,a2,…,axa_1,a_2,\dots,a_x。 总长度等式:

∑i=1xai+(x−1)=L  ⟹  ∑ai=L−(x−1)\sum_{i=1}^x a_i + (x-1)=L \implies \sum a_i = L-(x-1)

每个 ai≥2a_i\ge2,先给每个分配基础 22。 剩余可以额外分配的格子:

rem=L−(x−1)−2x=L+1−3xrem = L-(x-1)-2x = L+1-3x

根据琴生不等式,平方和要最小,数字要尽可能平均。 把 remrem 个格子平分给 xx 个冲刺: q=rem/xq=rem/x,md=rem%xmd=rem\%x。

  • mdmd 个冲刺长度:2+q+12+q+1
  • x−mdx-md 个冲刺长度:2+q2+q

冲刺代价是各段平方相加,走路只有 x−1x-1 步,代价为 c⋅(x−1)c\cdot(x-1)。 [ f(x)= md\cdot(2+q+1)^2 + (x-md)\cdot(2+q)^2 + c\cdot(x-1) ] 特别地,x=0x=0 代表完全走路,f(0)=L⋅cf(0)=L\cdot c。

f(x)f(x) 是单谷函数,先减小后增大,可以二分找最小值点。

注意数值极大,全程要用 __int128,手写输出。

代码

C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<class T>
void ckmx(T& a, T b) {
    a = max(a, b);
}
template<class T>
void ckmn(T& a, T b) {
    a = min(a, b);
}

template<class T>
void wrint(T x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x >= 10) {
        wrint(x / 10);
    }
    putchar(x % 10 ^ 48);
}

template<class T>
void wrintln(T x) {
    wrint(x);
    putchar('\n');
}

template<class T>
void read(T& x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
}

const int MAX_LIMIT = 200000;
int node_cnt;
vector<pii> graph_edge;

void add_edge(int from, int to) {
    graph_edge.emplace_back(make_pair(from, to));
}

int val_arr[MAX_LIMIT + 5];

int build_const_node(int k) {
    int cur_id = ++node_cnt;
    val_arr[cur_id] = k;
    for (int i = 0; i <= k - 1; i++) {
        add_edge(cur_id, build_const_node(i));
    }
    return cur_id;
}

int build_odd_node(int k) {
    int cur_id = ++node_cnt;
    val_arr[cur_id] = 0;
    for (int i = 0; i <= k - 1; i++) {
        add_edge(cur_id, build_const_node(i));
    }
    return cur_id;
}

int build_delay_node(int k) {
    if (k == 1) {
        int x = ++node_cnt;
        int y = ++node_cnt;
        int z = ++node_cnt;
        add_edge(x, y);
        add_edge(y, z);
        val_arr[x] = 1;
        val_arr[y] = 0;
        val_arr[z] = 0;
        return x;
    }
    int main_node = ++node_cnt;
    val_arr[main_node] = k;
    for (int i = 0; i <= k - 2; i++) {
        add_edge(main_node, build_odd_node(i));
    }

    int sub_node = ++node_cnt;
    val_arr[sub_node] = 0;
    for (int i = 0; i <= k - 3; i++) {
        add_edge(sub_node, build_const_node(i));
    }
    add_edge(sub_node, build_delay_node(k - 2));
    add_edge(sub_node, main_node);
    return main_node;
}

void run_test_case(int test_num) {
    int root = build_delay_node(17);
    for (int idx = node_cnt + 1; idx <= MAX_LIMIT; idx++) {
        add_edge(root, idx);
        val_arr[idx] = 1000000000;
    }
    for (int idx = 1; idx <= MAX_LIMIT; idx++) {
        printf("%d ", val_arr[idx]);
    }
    putchar('\n');
    for (auto &e : graph_edge) {
        int u = e.first;
        int v = e.second;
        printf("%d %d\n", u, v);
    }
}

int main() {
    int total_case = 1;
    for (int t = 1; t <= total_case; t++) {
        run_test_case(t);
    }
    return 0;
}
原文