当前位置:首页 > 技术 > 正文内容

Codeforces Round #708 (Div. 2) 解题报告 A, B, C1, C2, E1, E2

访客 技术 2026年7月7日 1

A. 最小排除值最大化

给定一个包含n个元素的数组,重新排序以求所有前缀的最大最小排除值和。如果一个前缀的最小排除值为x,则其自身的最小排除值应大于等于x。因此,较大的最小排除值越早出现越好,即顺序为0,1,2,3,...,后面的随意。

#include 
using namespace std;

const int MAXN = 2005;
int num[MAXN];

void solve() {
    memset(num, 0, sizeof(num));
    int n; cin >> n;
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        num[x]++;
    }
    int maxMex = 0;
    for (int i = 0; i <= 103; ++i) {
        if (num[i] == 0) {
            maxMex = i;
            break;
        }
    }
    for (int i = 0; i < maxMex; ++i) {
        cout << i << " ";
        num[i]--;
    }
    for (int i = 0; i <= 100; ++i) {
        while (num[i]--) cout << i << " ";
    }
    cout << endl;
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

B. M-数组

给定一个包含n个数字的数组和一个m值,要求将这个数组划分成最少的子数组,使得每个子数组中任意两个相邻元素之和为m的倍数(单个元素的子数组默认满足)。

#include 
using namespace std;

const int MAXN = 100005;
int cnt[MAXN];

void solve() {
    int n, m; cin >> n >> m;
    memset(cnt, 0, sizeof(cnt));
    vector<int> arr(n);
    for (auto &x : arr) {
        cin >> x;
        if (x % m == 0) continue;
        cnt[x % m]++;
    }
    int ans = 0;
    for (int i = 1; i < m; ++i) {
        if (!cnt[i]) continue;
        if (i * 2 == m) {
            ans++;
            continue;
        }
        int d = m - i;
        if (cnt[d] > cnt[i]) swap(cnt[d], cnt[i]);
        if (cnt[i] - cnt[d] <= 1) {
            ans += 1;
        } else {
            cnt[i] -= (cnt[d] + 1);
            ans += cnt[i] + 1;
        }
        cnt[d] = 0;
        cnt[i] = 0;
    }
    cout << ans + 1 << endl;
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

C1. k-LCM (简单版)

给定一个数n,找出三个数x, y, z,满足x + y + z = n且lcm(x, y, z) ≤ n / 2。

#include 
using namespace std;

void solve() {
    int n; cin >> n;
    if (n % 2) {
        cout << 1 << " " << n / 2 << " " << n / 2 << endl;
    } else {
        if ((n - 2) % 4 == 0) {
            cout << 2 << " " << (n - 2) / 2 << " " << (n - 2) / 2 << endl;
        } else {
            cout << n / 2 << " " << n / 4 << " " << n / 4 << endl;
        }
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

C2. k-LCM (复杂版)

给定一个数n和k,找出k个数(k ≥ 3),满足a_1 + a_2 + ... + a_k = n且lcm(a_1, a_2, ..., a_k) ≤ n / 2。

#include 
using namespace std;

void solve() {
    int n, k; cin >> n >> k;
    int s = k - 3;
    n -= s;
    for (int i = 0; i < s; ++i) cout << 1 << " ";
    if (n % 2) {
        cout << 1 << " " << n / 2 << " " << n / 2 << endl;
    } else {
        if ((n - 2) % 4 == 0) {
            cout << 2 << " " << (n - 2) / 2 << " " << (n - 2) / 2 << endl;
        } else {
            cout << n / 2 << " " << n / 4 << " " << n / 4 << endl;
        }
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

E1. 无平方因子分割 (简单版)

给定一个包含n个元素的数组,找到最少能将数组分成几段,使得每段中的任意两个数的乘积不是平方数。

#include 
using namespace std;

map mp;

int divide(int x) {
    int res = 1;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            int s = 0;
            while (x % i == 0) s++, x /= i;
            if (s & 1) res *= i;
        }
    }
    if (x != 1) res *= x;
    return res;
}

void solve() {
    mp.clear();
    int n; cin >> n;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int sum = divide(cin.get());
        if (mp.count(sum)) {
            ans++;
            mp.clear();
        }
        mp[sum] = 1;
    }
    cout << ans + 1 << endl;
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

E2. 无平方因子分割 (复杂版)

与上一题相同,但增加了最多可以修改其中k个数的条件。

#include 
using namespace std;

const int MAXN = 200005;
int mask[MAXN], lef[MAXN][22], cnt[MAXN];

int divide(int x) {
    if (mask[x]) return mask[x];
    int res = 1, c = x;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            int s = 0;
            while (x % i == 0) s++, x /= i;
            if (s & 1) res *= i;
        }
    }
    if (x != 1) res *= x;
    mask[c] = res;
    return res;
}

void solve() {
    int n, k; cin >> n >> k;
    vector<int> arr(n);
    for (auto &x : arr) x = divide(cin.get());
    for (int i = 0; i <= k; ++i) {
        int L = 1, sum = 0;
        for (int j = 1; j <= n; ++j) {
            cnt[arr[j]]++;
            if (cnt[arr[j]] > 1) sum++;
            while (sum > i) {
                cnt[arr[L]]--;
                if (cnt[arr[L]] >= 1) sum--;
                L++;
            }
            lef[j][i] = L;
        }
        while (L <= n) cnt[arr[L++]]--;
    }
    int dp[MAXN][22];
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i)
        for (int j = 0; j <= k; ++j)
            for (int q = 0; q <= j; ++q)
                dp[i][j] = min(dp[i][j], dp[lef[i][q] - 1][j - q] + 1);
    int ans = 2 * n;
    for (int i = 0; i <= k; ++i) ans = min(ans, dp[n][i]);
    cout << ans << endl;
}

int main() {
    int t; cin >> t;
    while (t--) solve();
    return 0;
}

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。