首页 > 其他分享 >AtCoder Beginner Contest 326 (ABC326)

AtCoder Beginner Contest 326 (ABC326)

时间:2023-11-04 16:46:01浏览次数:40  
标签:AtCoder 10 int mid ABC326 nowx nowy ans 326

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;
}

其他的不会,我是菜鸡。

标签:AtCoder,10,int,mid,ABC326,nowx,nowy,ans,326
From: https://www.cnblogs.com/yhx0322/p/17809512.html

相关文章

  • AtCoder Beginner Contest 224 H Security Camera 2
    洛谷传送门AtCoder传送门直接糊一手线性规划对偶板板。要求:\[\min\sumA_il_i+\sumB_ir_i\]\[\foralli,j,l_i+r_j\geC_{i,j}\]\[l_i,r_i\ge0\]\[l_i,r_i\in\mathbb{Z}\]可以证明\(l_i,r_i\)为整数的限制可以去掉,因为取到最优解时\(l_i,r_i\)一......
  • AtCoder Beginner Contest(abc) 314
    B-Roulette难度:⭐题目大意有一个猜数字的游戏,有n个人参加,每人都猜了若干个数;最后给出答案数字;在所有猜中数字的人中输出猜数数量最少的人的编号;(可能不止一个);解题思路数据不大,暴力即可;神秘代码#include<bits/stdc++.h>#defineintlonglong#def......
  • [ABC326D] ABC Puzzle 题解
    题意:给定整数\(N\),字符串\(R,C\),构造满足以下条件的\(N\timesN\)矩阵:1.每一行和每一列中\(A,B,C\)各有且仅有一个。2.第\(i\)行的第一个字母等于字符串\(R\)的第\(i\)个字符。3.第\(i\)列的第一个字母等于字符串\(C\)的第\(i\)个字符。看数据范围考虑应该......
  • AtCoder Beginner Contest 326 F
    F-RobotRotation一句话不开LL,见祖宗感谢大佬,和洛谷上的题解上面已经将的很清楚了,但是如果你跟我一样一开始看不懂他们的代码,那么这篇可能为你解惑点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglong#defineintLL//LL!map<LL,LL>ma;......
  • AtCoder Beginner Contest(abc) 312
    B-TaKCode难度:⭐题目大意题目定义一种矩阵X:该矩阵的是一个长度为9的由黑白色块组成正方形矩阵;该矩阵的左上角和右下角都是一个3*3的黑色矩阵(总共18个),这两个黑色矩阵外面(边缘不算)包围一圈白色色块(总共14个);现在一个n*m的黑白矩阵,问这个大矩阵中有多少......
  • 《AT_abc326_g Unlock Achievement》解题报告
    考场上压根没想到网络流,感觉这题是做过的网络流里算质量比较高的了。首先我们肯定是想直接贪心,但是发现怎么贪心都没办法,而且数据范围非常小,一般数据范围非常小,且贪心不了但又只能贪心的题就用网络流实现。考虑如何建模,首先我们发现权值有正有负,考虑最大权闭合子图,正权值连汇点,......
  • AT_abc326_d ABC Puzzle 题解
    AT_abc326_dABCPuzzle题解看题事实上,即使在\(N=5\)的情况下,也只有\(66240\)个网格满足「每行/每列恰好包含一个A、B和C」。——官方题解其实看到这道题,就感觉是搜索,这很显然。但是我们会发现,最最最native的搜索,是\(4^{5\times5}=2^{50}\)的。感觉不大可过,但是......
  • AT_abc326_e Revenge of "The Salary of AtCoder Inc." 题解
    AT_abc326_eRevengeof"TheSalaryofAtCoderInc."题解一道简单的概率论+动态规划题目(然而我赛时没看这道题题意有一个长度为\(n\)的序列\(A\)、一个\(n\)面骰子,掷若干次骰子,如果这一次掷骰子的点数小于等于上一次的点数,则结束。定义这若干次掷骰子的总的结果为,每次......
  • AT_abc326_f Robot Rotation 题解
    AT_abc326_fRobotRotation题解经典问题,以前遇到过一个类似的问题:[ABC082D]FTRobot。建议对比着看一看这两道题,是两种不同的思路。(那一道题不用输出方案,因此可以用bitset优化;而此题需要输出方案,因此需要双向搜索。思路注意到每次只能「左转」和「左转」。所以,偶数次走......
  • AtCoder Beginner Contest(abc) 311
    B-VacationTogether难度:⭐题目大意给定n个人的工作计划,'o'表示这天休息,'x'表示工作;请找出一段最长的所有人都休息的连续休息的天数;解题思路数据不大,暴力即可;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio......