跳到正文

个人记录

Perfect Judgement

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

文章信息

发布
2026年5月1日
收录
2026年5月1日
更新
2026年11月12日
篇幅
约 8 分钟

版本

  1. v3当前
  2. v2
  3. v1

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

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

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

  • tj−pj≥ti−pi+2wt_j - p_j \ge t_i - p_i + 2w
  • tj+pj≥ti+pi+2wt_j + p_j \ge t_i + p_i + 2w

令 ai=ti−pia_i = t_i - p_i,bi=ti+pib_i = t_i + p_i,那么条件就是 aj≥ai+2wa_j \ge a_i + 2w 且 bj≥bi+2wb_j \ge b_i + 2w。

于是问题转化成了:在 mm 个点 (ai,bi)(a_i, b_i) 中,找最长序列使得 aa 和 bb 都严格递增且差距至少 2w2w。

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

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

AC Code
C++
#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;
}
AC
C++
#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;
}
原文