Perfect Judgement

· · 个人记录

首先注意到题目要求的是在时间递增的情况下,选出尽可能多的音符,使得相邻两个都能按出 Perfect。

考虑两个音符 i 和 j(i < j),能连续按出的条件是什么?手指要从 p_i 移动到 p_j,花费 |p_i - p_j| 毫秒,而两个音符的可击打区间分别是 [t_i - w, t_i + w] 和 [t_j - w, t_j + w]。最紧的情况是尽量晚按 i、尽量早按 j,所以需要 (t_j - w) - (t_i + w) \ge |p_i - p_j|,即 t_j - t_i \ge |p_i - p_j| + 2w。

去掉绝对值,这个条件等价于同时满足:

令 a_i = t_i - p_i,b_i = t_i + p_i,那么条件就是 a_j \ge a_i + 2w 且 b_j \ge b_i + 2w。

于是问题转化成了:在 m 个点 (a_i, b_i) 中,找最长序列使得 a 和 b 都严格递增且差距至少 2w。

但注意 a 也要满足差距至少 2w,所以排序后需要分组处理:相同 a 的点不能互相转移,要等这一层全部查询完再统一更新。

最终答案就是所有 dp 的最大值。时间复杂度 O(m \log m)。

::::success[AC Code]


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

const int N = 1e5 + 5;

struct Note {
    int p;
    long long t;
} a[N];

int n, m, w;
int dp[N];
int bit[N * 2];
vector<long long> vals;

void update(int x, int val) {
    for (; x <= 2 * m; x += x & -x) bit[x] = max(bit[x], val);
}

int query(int x) {
    int res = 0;
    for (; x > 0; x -= x & -x) res = max(res, bit[x]);
    return res;
}

int main() {
    cin >> n >> m >> w;
    for (int i = 1; i <= m; i++) {
        cin >> a[i].p >> a[i].t;
    }

    for (int i = 1; i <= m; i++) {
        vals.push_back(a[i].t + a[i].p);
    }
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());

    int ans = 0;
    for (int i = 1; i <= m; i++) {
        int pos = lower_bound(vals.begin(), vals.end(), a[i].t + a[i].p) - vals.begin() + 1;
        dp[i] = query(pos) + 1;
        update(pos, dp[i]);
        ans = max(ans, dp[i]);
    }

    cout << ans << endl;
    return 0;
}
::::

::::success[AC]

```cpp
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

const int MAXN = 1e5 + 10;
const int ST_SIZE = 4 * MAXN; 
const ll INF = 1e18;


struct N {
    int p;
    ll t, L, R;
} nd[MAXN];


struct ST {
    int l, r;       
    int mc;         
    ll mt;          
} tr[ST_SIZE];


inline void pu(int u) {
    tr[u].mc = max(tr[u<<1].mc, tr[u<<1|1].mc);
}


void bd(int u, int l, int r) {
    tr[u].l = l;
    tr[u].r = r;
    tr[u].mc = 0;
    tr[u].mt = -INF;
    if (l == r) return;
    int m = (l + r) >> 1;

    if (u > ST_SIZE / 2) return;
    bd(u<<1, l, m);
    bd(u<<1|1, m+1, r);
}


void up(int pos, int c, ll t) {
    int u = 1;
    while (tr[u].l != tr[u].r) {
        int m = (tr[u].l + tr[u].r) >> 1;
        if (pos <= m) u = u << 1;
        else u = u << 1 | 1;
    }

    if (c > tr[u].mc || (c == tr[u].mc && t < tr[u].mt)) {
        tr[u].mc = c;
        tr[u].mt = t;
    }

    u >>= 1;
    while (u >= 1) {
        pu(u);
        u >>= 1;
    }
}


int qy(int pos, ll L) {
    int res = 0;

    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        q.pop();

        if (tr[u].l == tr[u].r) {
            ll cost = abs(pos - tr[u].l);
            if (tr[u].mt != -INF && tr[u].mt + cost <= L) {
                res = max(res, tr[u].mc);
            }
            continue;
        }

        q.push(u<<1);
        q.push(u<<1|1);
    }
    return res;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, m, w;

    if (!(cin >> n >> m >> w)) {
        cout << "0\n";
        return 0;
    }


    for (int i = 1; i <= m; i++) {
        cin >> nd[i].p >> nd[i].t;
        nd[i].L = max(0LL, nd[i].t - w); 
        nd[i].R = nd[i].t + w;
    }


    n = min(n, MAXN - 10);
    bd(1, 1, n);
    int ans = 0;

    for (int i = 1; i <= m; i++) {
        int p = nd[i].p;

        if (p < 1 || p > n) continue;
        ll L = nd[i].L;


        int mx = qy(p, L);
        int cur = mx + 1;
        ans = max(ans, cur);
        up(p, cur, L);
    }

    cout << ans << '\n';
    return 0;
}
::::