首页 > 其他分享 >P2487 [SDOI2011] 拦截导弹 题解

P2487 [SDOI2011] 拦截导弹 题解

时间:2024-02-28 23:45:37浏览次数:24  
标签:return int 题解 LDS SDOI2011 template P2487 include dp

题目链接:拦截导弹

约定:

本题中提到的 \(LDS\) 和 \(LIS\) 不是严格单调,而是非严格单调,即为 \(\le 或者 \ge\)。

比较神奇的题,问的东西比较多,我们逐渐拆分:

对于第一个问题而言,这个 dp 方程是很好写的:

\[dp[i]=\max{dp[j]}+1 (i<j,h[i] \le h[j],v[i]\le v[j]) \]

其中 \(dp[i]\) 即为以 \(i\) 结尾的最多导弹数。那么答案显然即为 \(\max{dp}\),这玩意和最长递增子序列那些都是差不多的,注意到转移条件为三维偏序,那么我们使用 \(cdq分治\) 优化成 \(n\log^2{n}\) 就行了,比较经典。

注意第二个问题问的概率,对于某个导弹被记录的概率,首先需要知道包括这个导弹的总方案数,以及总方案数中有多少个方案是可以击落的。注意,此时此刻问的方案数都是基于第一问的最长的那个 \(LDS\) 的基础上而言,所以我们还要考虑当前点是否有 \(LDS\) 的构造方案。

听上去比较复杂是吧,我们注意到一个 \(LDS\) 可以被拆为两个 \(LDS\) 拼接。

如图所示,如果 \(i\) 是在 \(LDS\) 当中,那么一定可以分为两部分,以 \(i\) 即为的 \(LDS\) 和以 \(i\) 开头的 \(LDS\),如果我们的红色方案数为 \(x\),蓝色的为 \(y\),则包含 \(i\) 的 \(LDS\) 的方案数即为 \(x \times y\)。而总方案数,其实在第一个问题当中,也可以一并算出来了。

注意到难点,关于 \(i\) 开头的 \(LDS\) 并无法直接 \(dp\) 得到,因为我们的 \(dp\) 为了维持无后效性,那么思考下以 \(i\) 开头的 \(LDS\) 如果我们从右往左看,它就是以 \(i\) 结尾的 \(LIS\),那么从右往左 \(dp\) 就对了:

\[dp[i]=\max{dp[j]}+1 (i<j,h[i]\ge h[j],v[i] \ge v[j]) \]

严格地注意严格不等式和非严格不等式,只需要 \(bit\) 维护最值 \(dp\) 和对应方案数即可。做两遍 \(cdq\) 优化这两者 \(dp\)。像第一维这种和下标有关的序,我们直接按照下标序写就行了,这样就不需要再单独写这个序的排序函数。当然值域很大,顺便离散化一下。

这题码量重复性的东西比较多,码量不小,简单说说。

对于 \(cdq分治\) 优化 dp 具体经典思路,是基于中序遍历分治树,在访问完左子树时开始更新,注意一点,远偏移量为了保证第一层偏序,所以并不能直接排序,因为我们不是传统的 \(cdq\) 分治为后序遍历分治树,在中序遍历右子树时,如果还是按以往那种偏序,会破坏掉第一层偏序,导致访问右子树时计算不出答案。我们通常做法是记录下标,然后将下标按照序进行排序,这样第一层偏序就会在统计时依旧保证正确。这是一个经典的套路,用 \(cdq分治\) 优化带有偏序转移的 \(dp\) 问题。

关于用线段树还是树状数组好,一般树状数组就行了,其实这个问题抽象一下,询问一块值域内,最大的区间最大值,以及区间最大值的个数。这玩意线段树很好写 pushUp,分讨一下就行了,树状数组改为维护前后缀最值就行了。一般只涉及前后缀最值,都习惯用树状数组。

最终统计答案时,很显然只有前后缀最长拼起来:

\[pre[i]+suf[i]-1=maxLen,才需要统计它所包含的方案数 \]

\[ans=\frac{PreCnt \times SufCnt}{AllCnt},数量很多,我们可以考虑直接double存就行了 \]

最后,值域很大,老规矩离散化一下,然后注意第二组 \(dp\) 的第一维偏序,要取反,所以我们取答案的时候也是取反后的下标去取。然后用 stable_sort 可以避免严格偏序问题,可以省一些代码,我个人很多合在一起写了,代码上很简略。当然手写归并数组之类的也行,本处直接调稳定排序函数了。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>()(p.second);
    }
};

constexpr int N = 5e4 + 10;
typedef pair<int, double> pid;

inline void Max(pid& x, const pid& y)
{
    if (x.first < y.first)x = y;
    else if (x.first == y.first)x.second += y.second;
}

int idx;
hash2<int, int, Hash> mp;

enum Type
{
    Pre = 0, Suf = 1
};

struct
{
    pid bit[2][N]; //0是前缀最值,1是后缀最值
    void add(int x, const pid& val, const Type& id)
    {
        if (id == Pre)while (x <= idx)Max(bit[id][x], val), x += lowBit(x);
        else while (x)Max(bit[id][x], val), x -= lowBit(x);
    }

    pid query(int x, const Type& id) const
    {
        pid ans;
        if (id == Pre)while (x)Max(ans, bit[id][x]), x -= lowBit(x);
        else while (x <= idx)Max(ans, bit[id][x]), x += lowBit(x);
        return ans;
    }

    void clear(int x, const Type& id)
    {
        constexpr pid zero;
        if (id == Pre)while (x <= idx)bit[id][x] = zero, x += lowBit(x);
        else while (x)bit[id][x] = zero, x -= lowBit(x);
    }
} Bit;

//1.hL>=hR,vL>=vR
//2.hL<=hR,vL<=vR
struct DP
{
    int h, v;
    pid ans;
} dp1[N], dp2[N];

int tmp[N];

inline bool cmp1(const int x, const int y)
{
    return dp1[x].h >= dp1[y].h;
}

inline bool cmp2(const int x, const int y)
{
    return dp2[x].h <= dp2[y].h;
}

inline void cdq(const int L, const int R, const Type type = Suf)
{
    const int mid = L + R >> 1;
    if (L == R)return;
    cdq(L, mid, type);
    auto dp = type == Suf ? dp1 : dp2;
    auto cmp = type == Suf ? cmp1 : cmp2;
    forn(i, L, R)tmp[i] = i;
    stable_sort(tmp + L, tmp + mid + 1, cmp), stable_sort(tmp + mid + 1, tmp + R + 1, cmp);
    int l = L;
    forn(r, mid+1, R)
    {
        while (l <= mid and cmp(tmp[l], tmp[r]))Bit.add(dp[tmp[l]].v, dp[tmp[l]].ans, type), l++;
        auto other = Bit.query(dp[tmp[r]].v, type);
        other.first++;
        Max(dp[tmp[r]].ans, other);
    }
    forn(i, L, l-1)Bit.clear(dp[tmp[i]].v, type);
    cdq(mid + 1, R, type);
}

int n;
set<int> ord;
int h[N], v[N];

inline void solve()
{
    cin >> n;
    forn(i, 1, n)
    {
        cin >> h[i] >> v[i];
        ord.insert(h[i]), ord.insert(v[i]);
    }
    for (const auto v : ord)mp[v] = ++idx;
    forn(i, 1, n)
    {
        h[i] = mp[h[i]], v[i] = mp[v[i]];
        dp1[i] = DP(h[i], v[i], pid(1, 1));
        dp2[n - i + 1] = DP(h[i], v[i], pid(1, 1));
    }
    cdq(1, n, Pre), cdq(1, n, Suf);
    pid ans;
    forn(i, 1, n)Max(ans, dp1[i].ans);
    auto [maxLen,cntAll] = ans;
    cout << maxLen << endl;
    cout << fixed << setprecision(5);
    forn(i, 1, n)
    {
        auto [pre,cnt1] = dp1[i].ans;
        auto [suf,cnt2] = dp2[n - i + 1].ans;
        cout << (pre + suf - 1 == maxLen ? cnt1 * cnt2 / cntAll : 0) << ' ';
    }
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为:\ O(n\log^2{V_{max}}) \]

标签:return,int,题解,LDS,SDOI2011,template,P2487,include,dp
From: https://www.cnblogs.com/Athanasy/p/18041729

相关文章

  • 题解 gym102900J 【Octasection】
    代码:#include<iostream>#include<algorithm>#include<stack>#include<vector>#include<cstdio>usingnamespacestd;typedefstructRectangle_tag{ intx1; intx2; inty1; inty2; Rectangle_tag(intx1_,intx2_,int......
  • 24冬网络流复习题解
    A是谁瞪了半个小时不会*2000呀,是谁呀是谁呀。感觉这题比部分紫题都难。。。首先发现选取字符的顺序并不重要,所以\(t\)可以看成\(26\)个字符要选的个数。设字符\(c\)出现了\(x\)次,那么直接向汇点连流量为\(x\)费用为\(0\)的边。然后考虑\(s_i\)与每个字符的关......
  • 2024年2月28号题解
    P4994终于结束的起点解题思路通过加法同余原理可以知道f[i]%m==0,那么f[i-1]%m=1,所有把f[i+1]%m=1转换成了f[i-1]%m=1的问题那么只需要填好斐波那契数列再判断就可以了,又因为斐波那契可能会超出int的范围那么我们对每一项斐波那契再取模就可以了代码......
  • 旅游景点 Tourist Attractions (壮压 DP)题解
    简化题意题目链接——不卡内存班题目链接——卡内存版给定\(n\)个点和\(m\)条边组成的无向图,按照一定限制要求停留\(2\simk+1\)共\(k\)个点(可以经过但不停留),求最短的从\(1\)出发到\(n\)的路径长。限制情况如下:共有\(q\)个限制,接下来\(q\)行,每行两个数\(x......
  • CF1408H Rainbow Triples 题解
    Description给定长度为\(n\)的序列\(p\)找出尽可能多的三元组\((a_i,b_i,c_i)\)满足:\(1\lea_i<b_i<c_i\len\)\(p_{a_i}=p_{c_i}=0\),\(p_{b_i}\ne0\)\(p_{b_i}\)互不相同。所有的\(a_i,b_i,c_i\)互不相同。输出最多可以选出多少个三元组,多组数据。\(\sumn\le......
  • P10160 [DTCPC 2024] Ultra 题解
    【题目描述】给你一个\(01\)序列,你可以进行如下操作若干次(或零次):将序列中形如\(101\cdots01\)的一个子串(即\(1(01)^k\),\(k\ge1\))替换成等长的\(010\cdots10\)(即\(0(10)^k\))。你要操作使得\(1\)的个数尽可能少,输出最少的\(1\)的个数。【思路】一开始看到这道题......
  • 数据类型拓展与面试题解读
    整数拓展进制:在平时咱们生活中经常见到的例如通用于电脑识别的二进制、咱们生活中常用的十进制、工作中常见的八进制与十六进制。二进制:通常会以0b开头十进制:咱们生活中的数字八进制:通常以0开头十六进制:通常以0x开头​ 浮点数拓展(float、double)试题举例银行......
  • Codeforces Round 929 (Div. 3) 题解(D-G)
    比赛链接:https://codeforces.com/contest/1933官解暂未放出。质量不错的一场D3。CF1933D.TurtleTenacity:ContinualMods题意将数组\(a_{1..n}\ge1\)重排,问是否能使\(((a_1\mathbin{\rmmod}a_2)\mathbin{\rmmod}a_3)\cdots\mathbin{\rmmod}a_n\ne0\)......
  • 题解 P10196【[USACO24FEB] Lazy Cow P】
    总算铂金组场切一题。似乎做麻烦了,而且常数蛮大的,但是没啥思维难度,记录一下。对于每一个需求,我们将其放置在平面直角坐标系的\((m_i,b_i)\)位置。另外,自然有一个\((0,0)\)需求,也同样处理这个点。我们需要支持插入一个点的操作,并维护出答案。先考虑不需要动态插点,只在最后求......
  • 第十四届蓝桥杯个人题解
    a幸运的数主要是思路 遍历1-100000000每一层循环,首先将其每一位分到数组里,并记录位数,如果是偶数位再接着往下,比较前半和后半是否相等:通过加减最后结果是否为零来判断intmain(){  intnum=0;  for(inti=1;i<100000000;i++)  {    ints[9]={0};......