AtCoder Beginner Contest 326 (ABC326)

发布时间 2023-11-04 16:42:47作者: yhx0322

A. 2UP3DOWN

直接模拟即可。

Code

B. 326-like Numbers

枚举,每次拆除百、十、个位,再判断。

Code

C. Peak

Description

数字线上放置了 \(N\) 个礼物。第 \(i\) 个礼物放置在坐标 \(A_i\) 处。

可以在数轴上选择长度为 \(M\) 的半开区间 \([x,x+M)\),并获得其中包含的所有礼物。

求:最多可以获得多少份礼物?

Solution

二分 / 双指针都可。

  • 二分答案: 二分出最多可以获得多少份礼物,答案为右边界。每次 check,枚举 \([1 \sim n]\),看看 \(a_{i+mid-1} - a_i\) 是否 \(< m\) 即可。

  • 双指针: 每次 \(l\)\([1 \sim n]\),用 while 循环枚举的最大右端点,即最大的满足 \(a_r - a_l \le m\) 的点,然后求 \(\max\)

Code

Solution 1

#include <bits/stdc++.h>


using namespace std;

const int N = 3e5 + 10;
int a[N];
int n, m;
map<int, int> mp[N];
set<int> st[N];

int l = 0, r, mid;

bool check(int mid) {
    for (int i = 1; i <= n - mid + 1; i++) {
        if (a[i + mid - 1] - a[i] < m) return true;
    }
    return false;
}

int main() {
    cin >> n >> m;
    r = n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    while (l <= r) {
        mid = (l + r) >> 1;
        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout << l - 1;
    return 0;
}

Solution 2

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 10;
int a[N];

int main() {
    int n, m, ans = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    int now = 1;
    for(int i = 1; i <= n; i++) { // 枚举左指针
        while(now < n && a[now + 1] - a[i] < m) now++; // 满足条件就一直自增
        ans = max(ans, now - i + 1);
    }
    cout << ans;
    return 0;
}

D. ABC Puzzle

Description

给定两个字符串 \(R\)\(C\) ,分别由 ABC 组成。

有一个 \(N \times N\) 网格。
在每个格中,最多只能写 ABC 中的一个字符。

确定是否可以满足以下所有条件,如果可以,打印。

  • 每行和每列恰好包含一个 A 、一个 B 和一个 C

  • \(i\) 行中最左边的字符与 \(R\) 的第 \(i\) 个字符匹配。

  • \(i\) 列中最上面的字符与 \(C\) 的第 \(i\) 个字符匹配。

Solution

暴力 DFS + 剪枝。

注意一些小优化的细节:

我们可以在 DFS 的过程中,判断目前搜索出来的方案是否合法,如果不合法,直接 return。

不能在搜索完毕之后在判断是否合法,否则时间复杂度极大,可能会被卡。

其他的就是搜索,没什么好说的。

Code

#include <bits/stdc++.h>

using namespace std;
int n, ans[10][10];
bool nowx[10][4], nowy[10][4];
string s, t;

void dfs(int x, int y) {
    if (x == n) {
        for (int i = 0; i < n; i++)
            if (nowx[i][3] + nowx[i][1] + nowx[i][2] + nowy[i][3] + nowy[i][1] + nowy[i][2] != 6) return;
        printf("Yes\n");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                if (!ans[i][j]) putchar('.');
                else printf("%c", 'A' - 1 + ans[i][j]);
            printf("\n");
        }
        exit(0);
    }
    int nx = x, ny = y + 1;
    if (ny >= n) nx++, ny = 0;
    if (!nowx[x][3] && !nowx[x][1] && !nowx[x][2]) {
        if ((nowy[y][3] || nowy[y][1] || nowy[y][2] || s[x] == t[y]) && (!nowy[y][s[x] - 'A' + 1])) {
            ans[x][y] = s[x] - 'A' + 1;
            nowx[x][s[x] - 'A' + 1] = 1;
            nowy[y][s[x] - 'A' + 1] = 1;
            dfs(nx, ny);
            nowx[x][s[x] - 'A' + 1] = 0;
            nowy[y][s[x] - 'A' + 1] = 0;
        }
        ans[x][y] = 0;
        dfs(nx, ny);
    } else {
        for (int i = 1; i <= 3; i++) {
            if ((!nowx[x][i]) && (nowy[y][3] || nowy[y][1] || nowy[y][2] || t[y] - 'A' + 1 == i) && (!nowy[y][i])) {
                ans[x][y] = i;
                nowx[x][i] = 1;
                nowy[y][i] = 1;
                dfs(nx, ny);
                nowx[x][i] = 0;
                nowy[y][i] = 0;
            }
        }
        ans[x][y] = 0;
        dfs(nx, ny);
    }
}

int main() {
    cin >> n >> s >> t;
    dfs(0, 0);
    printf("No");
    return 0;
}

其他的不会,我是菜鸡。