数据过弱
查看原帖
数据过弱
617130
Yorg楼主2024/12/14 17:03

以下代码过不了样例且随意 hack

但是他过掉了除样例之外的所有测试点

#include <bits/stdc++.h>
const int MAXN = 1e5 + 20;
const int MAXK = 120;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

int n, K;
int cnt = 0;
struct node {
    int l, r;
    bool meaningful = true;
    bool operator < (const node& a) {
        return (l == a.l) ? r > a.r : l < a.l;
    }
} Seq_Read[MAXN], Seq[MAXN];

int DIS[MAXN]; // 不相交最值 , 数组维护
struct Mono_Queue
{
    struct node {
        int Val;
        int timestamp;
    };
    std::deque<node> Q;

    void insert(node x) {
        while (!Q.empty() && Q.back().Val < x.Val) Q.pop_back();
        Q.push_back(x);
    }

    /*保持滑动区间, 顺手更新*/
    void update(int nowtimestamp, int id) {
        while (!Q.empty() && Seq[Q.front().timestamp].r < Seq[nowtimestamp].l) {
            DIS[id] = std::max(DIS[id], Q.front().Val + Seq[Q.front().timestamp].r);
            Q.pop_front();
        }
    }
} IS[MAXN]; // 相交最值 , 单调队列维护

/*对原序列进行一个去除无用线段*/
void Unique()
{
    for (int i = 1; i <= n; i++) {
        if (Seq_Read[i].l == Seq_Read[i - 1].l) Seq_Read[i].meaningful = false;
        else Seq_Read[i].meaningful = true;
    }
    for (int i = 1; i <= n; i++) {
        if (Seq_Read[i].meaningful) Seq[++cnt] = Seq_Read[i];
    }
    K -= n - cnt;
    n = cnt;
}

int f[MAXN][MAXK];

/*处理转移*/
void solve()
{
    K = std::max(K, 0); // 防止趋势
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= std::min(i - 1, K); j++) {
            IS[i - j - 1].update(i, i - j - 1); // 先更新当前的单调队列
            if (!IS[i - j - 1].Q.empty())
                f[i][j] = std::max(f[i][j], IS[i - j - 1].Q.front().Val + Seq[i].r);
            f[i][j] = std::max(f[i][j], DIS[i - j - 1] + Seq[i].r - Seq[i].l);
            IS[i - j].insert({f[i][j] - Seq[i].r, i});
        }
    }
    int Ans = 0;
    for (int i = 1; i <= n; i++) {
        Ans = std::max(Ans, f[i][K]);
    }
    printf("%d", Ans);
}

int main()
{
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i++)
        Seq_Read[i].l = read(), Seq_Read[i].r = read();
    std::sort(Seq_Read + 1, Seq_Read + n + 1);
    Unique();

    solve();

    return 0;
}
2024/12/14 17:03
加载中...