暑假马(码)力赛 2026.8.11

· · 个人记录

50 \times 50 = 250

学到了学到了,这可是rk1大佬说的,肯定是对的。

比赛基本情况

比赛整体回顾

本场比赛的前 4 题过程较为顺利,然而我在E题上面浪费了过多时间,导致F题最终没有骗到分。

如果当时我多花一点时间在F题上面的话,说不定还能做出来。然而当时我看排行榜大多数人都做的是E题,所以我当时就“想当然”地去磕E了,那时候应该抽出点时间认真看F的。

题目复盘

D题

我们不要被“冒泡”的外表迷惑,我们考虑数组中的任意两个点对,发现只要这个点对是逆序对,那么无论如何交换,这对点对答案始终有 1 的贡献。

然而,如果两个元素颜色相同,那么还要减一下。

用树状数组维护逆序对即可。

::::success[AC Code]

#include<bits/stdc++.h>
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define ll long long

using namespace std;

const int maxn = 3e5 + 5;
const int inf = 1e9;
const ll lnf = 2e18;

//c[j]!=c[i],x[j]<x[i] 

int n;
int c[maxn], x[maxn];
vector<int> col[maxn];
int f[maxn];

void add(int x, int k)
{
    while(x <= n)
        f[x] += k, x += (x & -x);
}

int query(int x)
{
    int res = 0;
    while(x)
        res += f[x], x &= x - 1;
    return res;
}

int main()
{
    scanf("%d", &n);
    pre(i, 1, n)
        scanf("%d", &c[i]);
    pre(i, 1, n)
        scanf("%d", &x[i]);
    pre(i, 1, n)
        col[c[i]].emplace_back(i);
    ll ans = 0;
    fpre(i, n, 1)
    {
        ans += query(x[i] - 1);
        add(x[i], 1);
    }
    memset(f, 0, sizeof f);
    pre(i, 1, n)
    {
        reverse(col[i].begin(), col[i].end());
        for(auto y : col[i])
        {
            ans -= query(x[y] - 1);
            add(x[y], 1);
        }
        for(auto y : col[i])
            add(x[y], -1);
    }
    printf("%lld\n", ans);
    return 0;
}

::::

时间复杂度

时间复杂度 O(n \log n)。

E题

赛事思路

我想的是枚举长度,然后选取那些离得比较进的元素,然后贪心。

::::error[错误代码]

#include<bits/stdc++.h>
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define ll long long

using namespace std;

const int maxn = 3e5 + 5;
const int inf = 1e9;
const ll lnf = 2e18;

int t, n;
string s;
int a[26], b[26], c[26], vis[26];
string ans;
int nowans;

void calc(int k)
{
    memset(c, 0, sizeof c);
    memcpy(b, a, sizeof a);
    memset(vis, 0, sizeof vis);
    set<pair<int,int>> s;
    pre(i, 0, 25)
        if(a[i])
            s.insert({a[i], i});
    int l = n / k;
    pre(i, 1, l)
    {
        if(s.empty())   break;
        vis[prev(s.end()) -> second] = 1;
        s.erase(prev(s.end()));
    }
//  pre(i, 0, 25)
//      if(vis[i])
//          printf("%c ", i + 'a');
//      else
//          printf("  ");
//  puts("");
//  pre(i, 0, 25)
//      printf("%d ", a[i]);
//  puts("");
    int cnt = 0, res = 0;
    string ans;
    for(auto y : ::s)
        if(vis[y - 'a'] && c[y - 'a'] < k)
            b[y - 'a']--, ans += y, c[y - 'a']++;
        else
        {
            bool ok = false;
            pre(i, 0, 25)
                if(vis[i] && c[i] + b[i] < k)
                {
                    ans += (i + 'a');
                    c[i]++;
                    b[y - 'a']--;
                    ok = true;
                    break;
                }
            if(ok)  continue;
            while(cnt < 25 && (vis[cnt] || c[cnt] == k))    ++cnt;
            ans += (cnt + 'a');
            c[cnt]++;
            b[y - 'a']--;
        }
    pre(i, 0, n - 1)
        res += (ans[i] != ::s[i]);
    if(res < nowans)
        ::ans = ans, nowans = res;
//  printf("calc(%d)=%s,%d\n", k, ans.c_str(), res);
}

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        cin>>s;
        memset(a, 0, sizeof a);
        for(auto y : s)
            a[y - 'a']++;
        nowans = inf;
        for(int i = 1;i * i <= n;++i)
            if(!(n % i))
            {
                calc(i);
                calc(n / i);
            }
        printf("%d\n", nowans);
        puts(ans.c_str());
    }
    return 0;
}

::::

赛后题解

我们考虑枚举使用多少种字符,那么每种字符的数量就是 \frac{n}{k}。

对于使用哪些字符,肯定是一开始的字符越多越好,这样还能让一些额外的字符转化为别的字符。

然后让多数量的字符补少数量的字符即可。

::::success[AC Code]

#include<bits/stdc++.h>
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define ll long long

using namespace std;

const int maxn = 3e5 + 5;
const int inf = 1e9;
const ll lnf = 2e18;

int t, n;
string s;
int a[26], b[26], vis[26];
string ans;
int nowans;

void calc(int k)
{
    memset(vis, 0, sizeof vis);
    vector<pair<int,int>> v;
    pre(i, 0, 25)
        v.emplace_back(i, a[i]);
    sort(v.begin(), v.end(), [&](const pair<int,int>&x, const pair<int,int>&y)
    {
        return x.second > y.second;
    });
    string ans = "";
    pre(i, 0, 25)
        if(i < k)
            b[v[i].first] = n / k, vis[v[i].first] = 1;
        else
            b[v[i].first] = 0;
    for(auto y : s)
        if(vis[y - 'a'] && b[y - 'a'])
            ans += y, b[y - 'a']--;
        else
            ans += ' ';
    for(auto& y : ans)
        if(y == ' ')
            pre(i, 0, 25)
                if(b[i])
                {
                    y = i + 'a';
                    --b[i];
                    break;
                }
    int res = 0;
    pre(i, 0, n - 1)
        res += (ans[i] != s[i]);
    if(res < nowans)
        nowans = res, ::ans.swap(ans);
}

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        cin>>s;
        memset(a, 0, sizeof a);
        for(auto y : s)
            a[y - 'a']++;
        nowans = inf;
        pre(i, 1, 26)
            if(!(n % i))
                calc(i);
        printf("%d\n", nowans);
        puts(ans.c_str());
    }
    return 0;
}

::::

F题

赛时思路

用dij来骗分,可惜没打完。

赛后题解

其实我的想法已经很接近正解了。

令 dis_{i,j} 表示当前在 i 这个节点上,有 j 块钱的最小时辰。

我们注意:钱最多只需要 2500 块!!!

由于最短路不会重复经过一个点(除非有负环),所以最短路最多经过 n-1 条边,所以银币的上限是有的。

然后正常dij即可。

时间复杂度 O((T+m)(\log_2(Tn)))

【数据删除】:《50*50=250》

::::success[AC Code]

#include<bits/stdc++.h>
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define ll long long

using namespace std;

const int maxn = 3e3 + 5;
const int inf = 1e9;
const ll lnf = 2e18;

int n, m, s;
struct Node
{
    int to, a, b;
};
vector<Node> edges[maxn];
ll dis[maxn][maxn];
int c[maxn], d[maxn];

void dijkstra(int a, int b)
{
    priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>> q;
    memset(dis, 0x3f, sizeof dis);
    dis[a][b] = 0;
    q.push({0, {a, b}});
    while(!q.empty())
    {
        ll _d = q.top().first;
        int x = q.top().second.first, y = q.top().second.second;
        q.pop();
        if(_d < dis[x][y])  continue;
        if(y + c[x] <= 2500)
            if(dis[x][min(2500, y + c[x])] > dis[x][y] + d[x])
                dis[x][min(2500, y + c[x])] = dis[x][y] + d[x], q.push(make_pair(dis[x][min(2500, y + c[x])], make_pair(x, min(2500, y + c[x]))));
        for(auto t : edges[x])
            if(y >= t.a && dis[t.to][y - t.a] > dis[x][y] + t.b)
                dis[t.to][y - t.a] = dis[x][y] + t.b, q.push(make_pair(dis[t.to][y - t.a], make_pair(t.to, y - t.a)));
    }
}

int main()
{
    scanf("%d%d%d", &n, &m, &s);
    pre(i, 1, m)
    {
        int u, v, a, b;
        scanf("%d%d%d%d", &u, &v, &a, &b);
        edges[u].push_back({v, a, b});
        edges[v].push_back({u, a, b});
    }
    pre(i, 1, n)
        scanf("%d%d", &c[i], &d[i]);
    dijkstra(1, min(s,2500));
    pre(i, 2, n)
        printf("%lld\n", *min_element(dis[i], dis[i] + 3001));
    return 0;
}

::::

整体反思