跳到正文

题解

ABC462 赛后总结

WRT_Partisan约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年6月13日
收录
2026年6月13日
更新
2026年11月14日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

A - Secret Numbers

题目大意

给你一个字符串 S(1≤∣S∣≤50)S(1\le\left|S\right|\le50),删除掉其中非数字字符,输出新字符串。

思路

与其删除非数字字符,不如只输出数字字符。

代码

C++
/*
 * @FilePath: \c++\ABC462A.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-13 19:40:22
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
string s;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> s;
    for (auto i : s)
        if (i >= '0' && i <= '9')
            cout << i;
    return 0;
}

B - Gift

题目大意

编号为 11 至 NN 的 N(2≤N≤100)N(2\le N\le100) 个人互相送礼物,第 ii 个人分别向第 Ai,1,Ai,2,…,Ai,KiA_{i,1},A_{i,2},\dots,A_{i,K_i} 个人送礼物,输出每个人收到的礼物个数以及都有谁给他送礼物。

思路

按照题意模拟即可。

代码

C++
/*
 * @FilePath: \c++\ABC462B.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-13 19:40:57
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k, a;
vector<int> mp[105];
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> k;
        for (int j = 1; j <= k; j++)
            cin >> a, mp[a].push_back(i);
    }
    for (int i = 1; i <= n; i++)
    {
        cout << mp[i].size() << " ";
        for (auto j : mp[i])
            cout << j << " ";
        cout << "\n";
    }
    return 0;
}

C - Not Covered Points

题目大意

给出 N(1≤N≤3×105)N(1\le N\le3\times10^5) 个点的坐标 (Xi,Yi)(1≤i≤N)(X_i,Y_i)(1\le i\le N),其中 {Xi},{Yi}\{X_i\},\{Y_i\} 均为 (1,2,…,N)(1,2,\dots,N) 的排列,求“在其左下方没有点”的点的个数(因为是排列所以不用考虑边界情况)。

思路

将点按照横坐标升序排序,从左到右对于每个点看在其之前是否有纵坐标比它小的点,如果没有则答案加一。

用 set 维护即可。

代码

C++
/*
 * @FilePath: \c++\ABC462C.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-13 19:41:04
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Node
{
    int x, y;
    bool operator<(const Node &a) const { return this->x < a.x; }
} a[300005];
int n, ans;
set<int> st;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].x >> a[i].y;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
    {
        auto it = st.upper_bound(a[i].y);
        if (it == st.begin())
            ans++;
        st.insert(a[i].y);
    }
    cout << ans;
    return 0;
}

D - Accomplice

题目大意

一场犯罪有 N(2≤N≤2×105)N(2\le N\le2\times10^5) 个嫌疑人,第 ii 个嫌疑人在时间 Si(1≤Si≤106)S_i(1\le S_i\le10^6) 进入房间,在时间 Ti(Si≤Ti≤106)T_i(S_i\le T_i\le10^6) 离开房间。

这场犯罪有两名罪犯,持续时间为 D(1≤D≤106)D(1\le D\le10^6),期间两名罪犯都要在房间内(两个临界时间点也被认为在房间内),求两名罪犯和犯罪开始时间有多少种可能组合。

思路

刚开始题读错了(翻译的锅),认为是罪犯的组合数,吭哧半天写了个带 tag 的线段树然后发现样例过不了。

对于每一个人来说,如果 Ti−Si<DT_i-S_i<D,那么他不可能犯罪,否则他犯罪开始时间点的范围为 [Si,Ti−D][S_i,T_i-D]。

用差分维护每个人犯罪开始时间点的范围,然后对于每个时间点,如果有 aa 个人可能开始犯罪,那么其对答案的贡献为 (a2)=a×(a−1)2\binom{a}{2}=\frac{a\times(a-1)}{2}。

代码

C++
/*
 * @FilePath: \c++\ABC462D.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-13 19:41:12
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
int n, d, s, t, ans, cf[1000005], sum;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> d;
    for (int i = 1; i <= n; i++)
    {
        cin >> s >> t;
        if (t - s >= d)
            cf[s]++, cf[t - d + 1]--;
    }
    for (int i = 1; i <= 1000000; i++)
        sum += cf[i], ans += sum * (sum - 1) / 2;
    cout << ans;
    return 0;
}

E - Alternating Costs

题目大意

T(1≤T≤2×105)T(1\le T\le2\times10^5) 次询问,每次询问如下:

给出一个点 (X,Y)(−109≤X,Y≤109)(X,Y)(-10^9\le X,Y\le10^9) 和 A,B(1≤A,B≤109)A,B(1\le A,B\le10^9),进行若干次操作使得一枚棋子从 (0,0)(0,0) 移动到 (X,Y)(X,Y)。

对于第 ii 次操作:

  • 如果 ii 是奇数,那么向左或向右移动的代价为 AA,向上或向下移动的代价为 BB。
  • 如果 ii 是偶数,那么向左或向右移动的代价为 BB,向上或向下移动的代价为 AA。

求从 (0,0)(0,0) 移动到 (X,Y)(X,Y) 所需的最小代价。

思路

首先向上走和向下走是一样的,向左走和向右走是一样的,所以先对坐标取绝对值。

为了方便计算,我们假设 A≥BA\ge B,如果不满足则交换 A,BA,B 同时交换 X,YX,Y。

X=YX=Y 显然是最好的(略微不准确,一些加一减一的情况也可以),一直用 AA 走即可。如果 X>YX>Y,那么有两种走法,一种是横向先走满 YY,剩下的 X−YX-Y 只能 AA 和 BB 交替;另一种是通过纵向多走几次,使得代价一直是 AA,大概就像长城那种样子。X<YX<Y 同理。

得到的两种总代价取小者即可,具体实现细节请看代码。

代码

C++
/*
 * @FilePath: \c++\ABC462E.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-13 19:41:20
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int t, a, b, x, y;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> t;
    while (t--)
    {
        cin >> a >> b >> x >> y, x = max(x, -x), y = max(y, -y);
        if (a > b)
            swap(a, b), swap(x, y);
        int num = max(y * 2, x * 2 - 1);
        num += num % 2 != (x + y) % 2;
        cout << min((x + y) * a + (b - a) * ((x + y) - min(x, (x + y + 1) / 2) - min(y, (x + y) / 2)), num * a) << "\n";
    }
    return 0;
}
原文