Loading

ABC481赛后总结

A

先求出 \(A+B\),然后求出位数即可。

#include <bits/stdc++.h>
using namespace std;
int a, b;
int ans(int x)
{
    int cnt = 0;
    while (x)
    {
        x /= 10;
        cnt++;
    }
    return cnt;
}
int main()
{
    cin >> a >> b;
    int tmp = a + b;
    cout << ans(tmp);
    return 0;
}

B

对于相邻的 \(2\) 个坐标 \((i,a_i)\) 和 \((i+1,a_{i+1})\),如果 \(y\) 在 \([a_i,a_{i+1}]\) 之间,则我们找到了一个纵坐标为 \(y\) 的点,但是如果端点处的点纵坐标为 \(y\),那么我们就会多算,因此可以把两部分的点的 个数分开计算,最后输出答案即可。

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define piii pair<pii, int>
#define pll pair<ll, ll>
#define plll pair<pll, ll>
#define pref static inline
#define fi first
#define se second
using namespace std;
const int MAXN = 105;
int n;
int a[MAXN];
int ans, y;
set<int> st;
namespace heffo_hard
{
    pref void solve()
    {
        cin >> n >> y;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        for (int i = 1; i < n; ++i)
        {
            if (y > min(a[i], a[i + 1]) && y < max(a[i], a[i + 1]))
            {
                ans++;
            }
        }
        for (int i = 1; i <= n; ++i)
            if (a[i] == y)
                ans++;
        cout << ans << endl;
    }
};

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    heffo_hard::solve();
    return 0;
}
/*
heffo_hard
*/

C

STL 大法好。
使用 unordered_map 来记录每个字符串所对应的 ID,如果对于输入的字符串没有找到,就新建账户,输出 ID 并记录,否则输出 unordered_map 中记录的 ID 即可。

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define piii pair<pii, int>
#define pll pair<ll, ll>
#define plll pair<pll, ll>
#define pref static inline
#define fi first
#define se second
using namespace std;
int n;
string s;
unordered_map<string, int> mp;
int cnt;
namespace heffo_hard
{
    pref void solve()
    {
        cin >> n;
        while (n--)
        {
            cin >> s;
            if (mp.find(s) == mp.end())
            { // 没找到
                mp[s] = ++cnt;
            }
            cout << mp[s] << endl;
        }
    }
};

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    heffo_hard::solve();
    return 0;
}
/*
heffo_hard
*/

D

首先考虑到对于一个星球,直接到达的行星中数量最少的行星代价为 \(0\),因此可以连一条长度为 \(0\) 的边,选择从当前行星通过路线直接到达的任何行星,代价为 \(1\),连一条长度为 \(1\) 的边,然后求出 \(S\) 到 \(T\) 的最短路即可,不能到达就输出 \(-1\) 即可。

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define piii pair<pii, int>
#define pll pair<ll, ll>
#define plll pair<pll, ll>
#define pref static inline
#define fi first
#define se second
using namespace std;
const int MAXN = 2E5 + 5;
int n, m, s, t;
int a, b;
int first[MAXN], nxt[2 * MAXN], to[2 * MAXN], w[MAXN * 2];
int dist[MAXN]; // 最短路
int tot;
set<int> st; // 索引
vector<int> v1[MAXN];
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
bool vis[MAXN];
namespace heffo_hard
{
    void add(int a, int b)
    {
        to[++tot] = b;
        nxt[tot] = first[a];
        first[a] = tot;
        w[tot] = 1;
    }
    void add1(int a, int b)
    {
        to[++tot] = b;
        nxt[tot] = first[a];
        first[a] = tot;
        w[tot] = 0;
    }
    void dijkstra(int s)
    {
        memset(dist, 0x3f, sizeof(dist));
        dist[s] = 0;
        q.push({0, s});
        while (!q.empty())
        {
            auto [d, t] = q.top();
            q.pop();
            if (vis[t])
                continue;
            vis[t] = 1;
            for (int i = first[t]; i; i = nxt[i])
            {
                int tmp = to[i];
                if (vis[tmp])
                    continue;
                if (dist[tmp] > dist[t] + w[i])
                {
                    dist[tmp] = dist[t] + w[i];
                    q.push({dist[tmp], tmp});
                }
            }
        }
    }
    pref void solve()
    {
        cin >> n >> m >> s >> t;
        memset(dist, -1, sizeof(dist));
        for (int i = 1; i <= m; ++i)
        {
            cin >> a >> b;
            st.insert(a);
            v1[a].push_back(b);
            // 考虑离线,先记录所有数对
        }
        // cout << "------------------------" << endl;
        for (int t : st)
        {
            sort(v1[t].begin(), v1[t].end()); // 排序
            // cout << t << ' ' << v1[t][0] << '\n';
            add1(t, v1[t][0]);
            for (int i = 1; i < v1[t].size(); ++i)
            {
                // cout << t << ' ' << v1[t][i] << '\n';
                add(t, v1[t][i]);
            }
        }
        dijkstra(s);
        if (dist[t] == 1061109567)
            cout << -1 << endl;
        else
            cout << dist[t] << endl;
    }
};

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    heffo_hard::solve();
    return 0;
}
/*
heffo_hard
*/
posted @ 2026-10-25 13:09  heffo_hard  阅读(6)  评论(0)    收藏  举报