Fragmentation merging(2021湖北省赛)

题目链接:Problem - D - Codeforces

题意:给定一个1-n的排列,选取两个区间,所选取的两个区间的下标不能重合(比如选[1,3]和[2,4]),把这两个区间合并后得到的集合必须是连续的。问有多少种选法。

思路:

N<5000考虑N^2做法,枚举合并后的集合C,判断其是否能被得到(也就是选最多两个区间能覆盖这个集合)。记录下数字对应的下标,枚举集合[i,j]是否能被选到,用一个数组记录下集合中对应元素的下标位置在哪,边枚举边维护覆盖集合C需要的区间数量。具体细节见注释。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e6 + 1;
const int mod = 998244353;
int a[N];
int b[N];
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        a[x] = i; // 存下标
    }
    if (n == 1)
    {
        cout << 0 << endl;
        return;
    }
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            b[j] = 0;
        }
        int cnt = 0;                 // cnt是覆盖集合C=[i,j]这个区间所需要选择的区间数量
        for (int j = i; j <= n; j++) // 判断C == i到j 这个集合能否被两个区间以下覆盖
        {
            int idx = a[j];
            if (b[idx - 1] == 0 && b[idx + 1] == 0) // 如果这个下标的左右都没被标记,说明这是一个新区间。
                cnt++;
            if (b[idx - 1] == 1 && b[idx + 1] == 1) // 左右都被标记过,说明能合并左右两个区间
                cnt--;
            // 其他情况也就是左边和右边有一个被标记过,说明可以这个数字可以合并到这个区间,cnt不变
            b[idx] = 1;
            if (cnt <= 2) // 如果覆盖集合C=[i,j]这个区间所需要选择的区间数量小于等2,说明这个集合合法
                res++;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        a[i] = 0;
        b[i] = 0;
    }
    cout << res << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
}

UDP分片是指将一个过大的UDP数据报分割成多个IPv4片段的过程。UDP分片卸载允许设备将超大的UDP数据报分割成多个IPv4片段。与TCP分段卸载的要求类似,但是对于分片后的IPv4片段,其IPv4标识符不应该递增,因为它们属于同一个UDP数据报的分片。\[1\] 关于UDP分片卸载的更多信息可以参考文档\[2\]。在链路层中,有一个最大传输单元(MTU)的概念,它限制了数据帧的最大长度。不同网络类型的MTU值不同,例如以太网的MTU是1500字节。当IP层需要传输的数据包长度超过MTU时,就需要对数据包进行分片操作,使每个分片的长度小于或等于MTU。UDP分片就是在这种情况下发生的。\[3\] 总结来说,UDP分片是将超大的UDP数据报分割成多个IPv4片段的过程,以适应链路层的MTU限制。每个分片都包含有关它们属于同一个UDP数据报的信息。 #### 引用[.reference_title] - *1* *2* [Kernel: net: udp: ufo,UDP fragment offload, UDP分片脱负](https://blog.csdn.net/qq_36428903/article/details/126394978)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [UDP可靠传输(KCP))](https://blog.csdn.net/asdaqqwc/article/details/122385671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值