【BZOJ2253】纸箱堆叠(CDQ分治,DP)

本文介绍了一个三维箱子问题的解决方案,使用CDQ分治算法解决三维严格小于另一个箱子的问题。文章详细介绍了算法的具体实现过程,包括数据结构的定义、比较函数的设计以及主要的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

https://www.lydsy.com/JudgeOnline/problem.php?id=2253


Solution

只有三维严格小于另一个箱子才可以转移,直接CDQ分治即可。


Code

/************************************************
 * Au: Hany01
 * Date: Sep 26th, 2018
 * Prob: BZOJ2253
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/
 
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
 
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
 
inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}
 
const int maxn = 5e4 + 5;
 
int base ,prd, MOD, n, ls[maxn], lss;
 
struct Box {
    int x, y, z, dp;
}a[maxn], tmp[maxn];
inline bool cmpx(Box A, Box B) { return A.x < B.x || (A.x == B.x && A.y > B.y) || (A.x == B.x && A.y == B.y && A.z > B.z); }
inline bool cmpy(Box A, Box B) { return A.y < B.y || (A.y == B.y && A.z > B.z); }
 
struct BIT {
    int c[maxn];
    inline void update(int x, int dt) { for ( ; x <= n; x += (x & -x)) chkmax(c[x], dt); }
    inline void recover(int x) { for ( ; x <= n; x += (x & -x)) c[x] = 0; }
    inline int query(int x) {
        int ans = 0;
        for ( ; x; x -= (x & -x)) chkmax(ans, c[x]);
        return ans;
    }
}bit;
 
void solve(int l, int r) {
    if (l == r) return;
    register int mid = (l + r) >> 1, cur1 = l, cur2 = mid + 1;
    solve(l, mid);
    stable_sort(a + mid + 1, a + r + 1, cmpy);
    for (register int tot = l; tot <= r; ++ tot) {
        if (cur1 > mid || (cur1 <= mid && cur2 <= r && a[cur1].y >= a[cur2].y))
            chkmax(a[cur2].dp, bit.query(a[cur2].z - 1) + 1), ++ cur2;
        else bit.update(a[cur1].z, a[cur1].dp), ++ cur1;
    }
    For(i, l, mid) bit.recover(a[i].z);
    stable_sort(a + mid + 1, a + r + 1, cmpx);
    solve(mid + 1, r);
    stable_sort(a + l, a + r + 1, cmpy);
}
 
int main()
{
#ifdef hany01
    freopen("bzoj2253.in", "r", stdin);
    freopen("bzoj2253.out", "w", stdout);
#endif
 
    base = read(), prd = 1, MOD = read(), n = read();
    For(i, 1, n) {
        prd = a[i].x = (LL)prd * base % MOD,
        prd = a[i].y = (LL)prd * base % MOD,
        prd = a[i].z = (LL)prd * base % MOD;
        if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
        if (a[i].x > a[i].z) swap(a[i].x, a[i].z);
        if (a[i].y > a[i].z) swap(a[i].y, a[i].z);
    }
 
    For(i, 1, n) ls[i] = a[i].x;
    stable_sort(ls + 1, ls + 1 + n), lss = unique(ls + 1, ls + 1 + n) - ls - 1;
    For(i, 1, n) a[i].x = lower_bound(ls + 1, ls + 1 + lss, a[i].x) - ls;
    For(i, 1, n) ls[i] = a[i].y;
    sort(ls + 1, ls + 1 + n), lss = unique(ls + 1, ls + 1 + n) - ls - 1;
    For(i, 1, n) a[i].y = lower_bound(ls + 1, ls + 1 + lss, a[i].y) - ls;
    For(i, 1, n) ls[i] = a[i].z;
    sort(ls + 1, ls + 1 + n), lss = unique(ls + 1, ls + 1 + n) - ls - 1;
    For(i, 1, n) a[i].z = lower_bound(ls + 1, ls + 1 + lss, a[i].z) - ls;
 
    For(i, 1, n) a[i].dp = 1;
    sort(a + 1, a + 1 + n, cmpx);
    //For(i, 1, n) cout << a[i].x << ' ' << a[i].y << ' ' << a[i].z <<endl;
    solve(1, n);
    int ans = 0;
    For(i, 1, n) chkmax(ans, a[i].dp);
    printf("%d\n", ans);
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值