勰码金牌夏令营补题报告3

· · 个人记录

勰码金牌夏令营补题报告3

刘慕贤

题目报告

$T2$ $5 pts$,时间:**1.5 h** $T3$ $15 pts$,时间:**1 h** $T4$ $18 pts$,时间:**1 h** ## 赛中概况 按顺序做,上来先做的 $T1$ ,就是手搓了几组样例,观察了一下,发现了规律,迅速写了,结果被取模卡了。然后看 $T2$,觉得可以是 $dp$ ,花了些许时间,结果没调出来,$T3$ 一看暴力像是最小生成树,然后写 $T4$ $25 pts$ 的部分分,结果有一档挂掉了 ## 解题报告 ### T1 熊喵喵打史莱姆(slime) #### 题目详情 **未通过**,$30 pts

错误之处

取模了,但只取模了一半

两个统计答案,只取模了一个

题目大意

熊喵喵要打一个最大血量为 n 的史莱姆。每回合,他可以选择一个仍然存活的史莱姆攻击一次,造成 k 点伤害。当史莱姆的当前血量小于等于 0 时,它会死亡。

如果一个史莱姆受到攻击后,当前血量小于等于最大血量的一半,并且仍未死亡,它就会分裂成两个史莱姆。两个新史莱姆的当前血量和最大血量都等于分裂前的当前血量。

史莱姆不会攻击,只会挨打。

求熊喵喵打掉所有史莱姆需要的回合数。答案可能很大,请输出答案对 100000007 取模后的结果。

你需要处理多组数据。

题目解析

清新小思维题

观察样例易得,史莱姆的分裂类似于树,只需要 log 的处理一条链再乘上对应层数的史莱姆数量即可

正解代码

奉上代码

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MOD = 100000007;

ll cd(ll _, ll __)
{
    return (_ + __ - 1) / __;
}

int main(){
    ios::sync_with_stdio(NULL);
    cin.tie(NULL); cout.tie(NULL);


    int _;
    cin >> _;

    while (_--)
    {
        ll n, k;
        cin >> n >> k;


        ll __ = 1;
        ll ___ = 0;

        while (n > 0)
        {
            ll need = (n + 1) / 2;
            ll cnt = cd(need, k);


            ___ = (___ + __ % MOD * cnt % MOD) % MOD;
            n -= (cnt * k);

            __ = (__ * 2) % MOD;


            if (n <= 0) break;
        }

        cout << ___ << '\n';
    }

    return 0;
}

T2 安可(encore)

题目详情

未通过,5 pts

错误之处

单调队列判断谁更劣的时候判断反了

题目大意

一只蝴蝶想飞过沧海。

沧海可以看作一个长度为 n 的 01 串。第 i 个位置为 1 表示蝴蝶可以在这里落脚休息,为 0 表示不可以

初始时蝴蝶位于 0 位置,它想飞到沧海另一边的 n + 1 位置。由于体力有限,相邻两次落脚位置之间的距离不能超过 m,这里也把 0 和 n + 1 看作落脚位置

作为魔法使的你可以进行至多 k 次变换。每次变换可以交换这个 01 串中的任意两个位置

请判断是否存在一种变换方案,使得变换后的串允许蝴蝶顺利飞过沧海。

题目解析

观察到 k ≤ 10, 由此,我们可以利用分层图最短路(dijikstra)解决

代码实现并不困难,见下

正解代码

奉上代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, K = 210;
int a[N], b[N], dp[N][K];
deque<int> q[N];
signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tid, T;
    cin >> tid >> T;
    while (T--)
    {
        int n, m, k;
        cin >> n >> m >> k;

        int cnt = 0;
        for (int i = 1; i <= n; i += 1)
        {
            char x; cin >> x;
            if (x == '0') a[i] = 0;
            else a[i] = 1, cnt += 1;
        }

        if ((n + 1) <= m)
        {
            cout << "Yes" << endl;
            continue;
        }

        for (int i = 0; i <= k; i += 1) while (!q[i].empty()) q[i].pop_front();
        for (int i = 0; i <= n; i += 1) for (int j = 0; j <= k; j += 1) dp[i][j] = 1e18 + 7;

        dp[0][0] = 0;
        q[0].push_back(0);

        for (int i = 1; i <= n; i += 1)
        {
            for (int j = 0; j <= k; j += 1)
            {
                while (!q[j].empty() and q[j].front() < (i - m)) q[j].pop_front();
            }

            for (int j = 0; j <= k; j += 1)
            {
                int from = j - (a[i] == 0);
                if (from < 0) continue;
                if (!q[from].empty())
                {
                    if (dp[q[from].front()][from] != (1e18 + 7)) dp[i][j] = dp[q[from].front()][from] + 1;
                }
            }

            for (int j = 0; j <= k; j += 1)
            {
                if (dp[i][j] == (1e18 + 7)) continue;
                while (!q[j].empty() and dp[q[j].back()][j] >= dp[i][j]) q[j].pop_back();
                while (!q[j].empty() and q[j].front() <= (i - m)) q[j].pop_front();
                q[j].push_back(i);
            }
        }

        bool res = false;
        for (int i = max(1ll, n - m + 1); i <= n; i += 1)
        {
            for (int j = 0; j <= k; j += 1)
            {
                if (dp[i][j] == (1e18 + 7) or dp[i][j] > cnt) continue;

                res = true;
                break;
            }
            if (res) break;
        }

        if (res == true) cout << "Yes" << endl;
        else cout << "No" << endl;

    }
    return 0;
}

T3 最大公约数查询 (gcd)

题目详情

未通过,18 pts

题目大意

哥萨克 Vus 得到了一个包含 n 个整数的数组 a,另有一个内容未知、长度同样为 n 的整数数组 b

为了确定 b, Vus 可以进行任意多次询问:

求保证能够确定整个数组 b 所需的最少戈比数

随后数组 a 会发生 q 次单点修改(有后效性),每次把某个 a_i 改为 x,请在每次修改后重新求出答案

题目解析

假如有一个前缀和数组,那么对于一次 l ~ r 的查询,本质上是得到了 sum_r 与 sum_{l-1} 的差,就可以建图了

这题的难点不是怎样猜 b 数组,而是怎样描述“一组询问足以唯一确定 b”。把区间和写成前缀和之差以后,这个条件会直接变成图的连通性

然后用一些差分约束就可以了

正解代码

奉上代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;
int a[4 * N];
void push_up(int o)
{
    a[o] = __gcd(a[o << 1], a[o << 1 | 1]);
}
void Build_tree(int o, int l, int r)
{
    if (l == r)
    {
        cin >> a[o];

        return;
    }

    int mid = l + r >> 1;
    Build_tree(o << 1, l, mid); Build_tree(o << 1 | 1, mid + 1, r);

    push_up(o);

    return;
}
void modify(int o, int l, int r, int id, int num) 
{
    if (l == r)
    {
        a[o] = num;

        return;
    }

    int mid = l + r >> 1;
    if (id <= mid) modify(o << 1, l, mid, id, num);
    else modify(o << 1 | 1, mid + 1, r, id, num);

    push_up(o);

    return;
}
int query(int o, int l, int r, int x, int y)
{
    if (l == r) return l;

    int p = __gcd(a[o << 1], x);
    int q = __gcd(a[o << 1 | 1], y);

    int mid = l + r >> 1;
    if (p <= q) return query(o << 1, l, mid, x, q);
    else return query(o << 1 | 1, mid + 1, r, p, y);
}
int calc_1(int o, int l, int r, int id, int num)
{
    if (l == r) return __gcd(a[o], num);

    int mid = l + r >> 1;
    int p = __gcd(a[o << 1 | 1], num);
    int q = __gcd(a[o << 1], p);

    int res = 0;
    if (id <= mid) res += calc_1(o << 1, l, mid, id, p);
    else
    {
        if (p == q) res += (mid - l + 1) * p;
        else res += calc_1(o << 1, l, mid, id, p);
        res += calc_1(o << 1 | 1, mid + 1, r, id, num);
    }

    return res;
}
int calc_2(int o, int l, int r, int id, int num)
{
    if (l == r) return __gcd(a[o], num);

    int mid = l + r >> 1;
    int p = __gcd(a[o << 1], num);
    int q = __gcd(a[o << 1 | 1], p);

    int res = 0;
    if (id > mid) res += calc_2(o << 1 | 1, mid + 1, r, id, p);
    else
    {
        if (p == q) res += (r - mid) * p;
        else res += calc_2(o << 1 | 1, mid + 1, r, id, p);
        res += calc_2(o << 1, l, mid, id, num);
    }

    return res;
}
signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, _;
    cin >> n >> _;

    Build_tree(1, 1, n);

    int p = query(1, 1, n, 0, 0);
    cout << calc_1(1, 1, n, p, 0) + calc_2(1, 1, n, p, 0) - a[1] << endl;

    while (_--)
    {
        int id, num; cin >> id >> num;

        modify(1, 1, n, id, num);

        int p = query(1, 1, n, 0, 0);
        cout << calc_1(1, 1, n, p, 0) + calc_2(1, 1, n, p, 0) - a[1] << endl;
    }
    return 0;
}

T4 ABC交换(abc)

题目详情

未通过,18 pts

错误之处

莫名奇妙的被一些小小的边界情况卡了

同时 a^xb^yc^z 形式的统计错了

题目大意

给定一个只包含字符 A、 B、 C 的字符串 s

一次操作中,可以选择两个相邻字符,只有当它们依次为 AB、 BC 或 CA 时,才可以交换这两个字符

求最多能够连续进行多少次操作

每个测试文件包含多组数据,各组数据相互独立

题目解析

正解太难,此处只展示暴力

正解代码

奉上代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
char a[N];
int tmp[N];
bool f(int x, int y)
{
    if (a[x] == 'A' and a[y] == 'B') return 1;
    else if (a[x] == 'B' and a[y] == 'C') return 1;
    else if (a[x] == 'C' and a[y] == 'A') return 1;
    else return 0;
}
int dfs_ans;
void dfs(string s, int cnt)
{
    dfs_ans = max(dfs_ans, cnt);

    for (int i = 1; i < s.size(); i += 1)
    {
        if ((s[i - 1] == 'A' and s[i] == 'B') or (s[i - 1] == 'B' and s[i] == 'C') or (s[i - 1] == 'C' and s[i] == 'A'))
        {
            swap(s[i - 1], s[i]);
            dfs(s, cnt + 1);
            swap(s[i - 1], s[i]);
        }
    }
}
signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;

        if (n <= 12)
        {
            dfs_ans = 0;

            string s;
            cin >> s;
            dfs(s, 0);
            cout << dfs_ans << endl;
            continue;
        } // 10 pts
        for (int i = 1; i <= n; i += 1) cin >> a[i];

        if (n <= 3)
        {
            if (n == 1) cout << 0 << endl;
            else if (n == 2)
            {
                if (f(1, 2)) cout << 1 << endl;
                else cout << 0 << endl;
            }
            else
            {
                int res = 0;
                if (f(1, 2))
                {
                    res += 1;
                    swap(a[1], a[2]);
                    if (f(2, 3)) res += 1;
                }
                else
                {
                    if (f(2, 3))
                    {
                        res += 1;
                        swap(a[2], a[3]);
                        if (f(1, 2)) res += 1;
                    }
                }

                cout << res << endl;
            }
            continue;
        } // 3 pts
        bool flag = true;
        for (int i = 1; i <= n; i += 1) if (a[i] == 'C') flag = false;
        if (flag == true)
        {
            int cnt = 0;
            for (int i = 1; i <= n; i += 1)
            {
                if (a[i] == 'A') cnt += 1;
                else tmp[i] = cnt;
            }

            int res = 0;
            for (int i = 1; i <= n; i += 1) res += tmp[i];

            cout << res << endl;
            continue;
        } // 5 pts
        int cnt = 0;
        for (int i = 2; i <= n; i += 1)
        {
            if (a[i - 1] == 'A' and a[i] == 'B')
            {
                if (cnt == 0) cnt += 1;
                else cnt = -1;
            }
            else if (a[i - 1] == 'B' and a[i] == 'C')
            {
                if (cnt == 1) cnt += 1;
                else cnt = -1;
            }
            else if ((a[i - 1] == 'A' and a[i] == 'A') or (a[i - 1] == 'B' and a[i] == 'B') or (a[i - 1] == 'C' and a[i] == 'C')) continue;
            else cnt = -1;

            if (cnt == -1) break;
        }
        if (cnt == 2)
        {
            int cnta = 1, cntb = 0, cntc = 0;
            for (int i = 2; i <= n; i += 1)
            {
                if (a[i] == 'A') cnta += 1;
                if (a[i] == 'B') cntb += 1;
                if (a[i] == 'C') cntc += 1;
            }

            cout << max(cnta * cntb, cntb * cntc) << endl;

            continue;
        } // 5 pts

        int ans = 0;
        for (int i = 2; i <= n; i += 1)
        {
            if ((a[i - 1] == 'A' and a[i] == 'B') or (a[i - 1] == 'B' and a[i] == 'C') or (a[i - 1] == 'C' and a[i] == 'A')) ans += 1;
        } // 10 pts
        cout << ans << endl;
    }
    return 0;
}

总结

一般

$T2$ 可惜没调出来正解 $T3$ 正常,写了 $15 pts$ 部分分 $T4$ 正常略有可惜,挂了一些小分