---
id: 474
lang: en
translation_of: index.md
source_sha256: 4d9df61089c0a8ee3d76a5d6d118e334c7536359d28e8ac5082b26e25e190cdd
title: "P2501: [HAOI2006] Number Sequence — Solution"
description: "This problem is really quite nasty. For the first question, subtract each element's own index while reading the sequence, then find the LIS length."
tags: ["OI","DP","Nasty Problems","Godlike","Adjacency List"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "剪去 (sic, “cut off”) read as 减去, “subtract”."
---

## Main Idea

This problem is really quite <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span>.

First, let's look at the first question. According to XG, <span class="slang" data-zh="巨佬" data-gloss="jùlǎo, “huge guy”. A very skilled contestant; used with admiration.">a big shot</span>, it's very obvious. While reading in the sequence, we subtract each element's own index from it, and then just find the length of the LIS. Code:

```cpp
scanf("%lld", &n);
for (ll i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), arr[i] -= i, dst[i] = INF;
dst[0] = -INF, dp[1] = 1, len = 1, dst[1] = arr[1], arr[0] = -INF, arr[++n] = INF;
for (ll i = 2; i <= n; i++)
{
    ll addr = upper_bound(dst, dst + 1 + len, arr[i]) - dst;
    len = max(len, addr);
    dp[i] = addr;
    dst[addr] = min(dst[addr], arr[i]);
}
printf("%lld\n", n - dp[n]);
```

For the second question, we bring in a small result: if we want to make the interval $[l\dots r]$ “nice-looking”, then this subinterval must split into two segments: a segment at height $arr[l]$ and a segment at height $arr[r]$. Note that by this point the elements in $arr[]$ have already had their indices subtracted.

So this second question is an interval DP, with time complexity $O(n^3)$. Luckily, though, the sequence is random, and we can use an adjacency list to store in advance the intervals that can be handled, and then do the DP.

## Code

```cpp
// P2501.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const ll MX_N = 35020, INF = 0x3f3f3f3f;
ll head[MX_N], arr[MX_N], n, len, dst[MX_N], dp[MX_N], M_curt, f[MX_N], sum1[MX_N], sum2[MX_N];
struct edge
{
    ll to, nxt;
} edges[MX_N << 1];
void addpath(ll src, ll dst)
{
    edges[M_curt].to = dst, edges[M_curt].nxt = head[src];
    head[src] = M_curt++;
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%lld", &n);
    for (ll i = 1; i <= n; i++)
        scanf("%lld", &arr[i]), arr[i] -= i, dst[i] = INF;
    dst[0] = -INF, dp[1] = 1, len = 1, dst[1] = arr[1], arr[0] = -INF, arr[++n] = INF;
    for (ll i = 2; i <= n; i++)
    {
        ll addr = upper_bound(dst, dst + 1 + len, arr[i]) - dst;
        len = max(len, addr);
        dp[i] = addr;
        dst[addr] = min(dst[addr], arr[i]);
    }
    printf("%lld\n", n - dp[n]);
    for (ll i = n; i >= 0; i--)
        addpath(dp[i], i), f[i] = INF;
    f[0] = 0;
    for (ll i = 1; i <= n; i++)
        for (ll e = head[dp[i] - 1]; e != -1; e = edges[e].nxt)
        {
            ll v = edges[e].to;
            if (v > i)
                break;
            if (arr[v] > arr[i])
                continue;
            for (ll k = v; k <= i; k++)
                sum1[k] = abs(arr[k] - arr[v]), sum2[k] = abs(arr[k] - arr[i]);
            for (ll k = v + 1; k <= i; k++)
                sum1[k] += sum1[k - 1], sum2[k] += sum2[k - 1];
            for (ll k = v; k <= i - 1; k++)
                f[i] = min(f[i], f[v] + sum1[k] - sum1[v] + sum2[i] - sum2[k]);
        }
    printf("%lld", f[n]);
    return 0;
}
```
