首页 > 其他分享 >ABC351

ABC351

时间:2024-04-27 23:55:23浏览次数:15  
标签:ni nj vector continue int rep ABC351

A. The bottom of the ninth

答案为 \(\sum a_i - \sum b_i + 1\)

B. Spot the Difference

模拟

C. Merge the balls

直接按题意模拟就是 \(O(n)\) 的,那么这题的瓶颈就在于两个球做合并,注意到 \(2^a + 2^a = 2^{a+1}\),那么我们就可以不考虑底数 \(2\),而直接处理指数即可

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<int> st;
    rep(i, n) {
        int a;
        cin >> a;
        while (st.size() >= 1 and st.back() == a) {
            st.pop_back();
            a++;
        }
        st.push_back(a);
    }
    
    cout << st.size() << '\n';
    
    return 0;
}

D. Grid and Magnet

不妨将磁铁周围的格子标记为 X,可以发现由剩下可通行的格子构成的同一个连通块里的格子的自由度一定是相同的,也就是这个连通块的大小再加上这个连通块四周的 X 的个数

可以用 \(\operatorname{bfs}\) 来处理

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
    int h, w;
    cin >> h >> w;
    
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    
    vector x(h, vector<bool>(w));
    rep(i, h)rep(j, w) {
        if (s[i][j] != '#') continue;
        x[i][j] = true;
        rep(v, 4) {
            int ni = i+di[v], nj = j+dj[v];
            if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
            x[ni][nj] = true;
        }
    }
    
    int ans = 1;
    vector used(h, vector<bool>(w));
    vector last(h, vector<int>(w)); int tm = 0;
    rep(si, h)rep(sj, w) {
        if (x[si][sj]) continue;
        if (used[si][sj]) continue;
        ++tm;
        int cnt = 0;
        queue<P> q;
        q.emplace(si, sj); used[si][sj] = true;
        while (q.size()) {
            auto [i, j] = q.front(); q.pop();
            cnt++;
            rep(v, 4) {
                int ni = i+di[v], nj = j+dj[v];
                if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
                if (used[ni][nj]) continue;
                if (x[ni][nj]) {
                    if (last[ni][nj] != tm) cnt++, last[ni][nj] = tm;
                    continue;
                }
                q.emplace(ni, nj);
                used[ni][nj] = true;
            }
        }
        ans = max(ans, cnt);
    }
    
    cout << ans << '\n';
    
    return 0;
}

标签:ni,nj,vector,continue,int,rep,ABC351
From: https://www.cnblogs.com/Melville/p/18162826

相关文章

  • ABC351G题解
    考虑动态dp的套路,先树剖一下,令\(son(x)\)为点\(x\)的重儿子。\(g_x=\prod\limits_{u\inC(x)\landu\neqson(x)}f_u\)。于是有\(f_x=f_{son(x)}g_x+a_x\)。于是\(\begin{bmatrix}f_x&1\end{bmatrix}=\begin{bmatrix}f_{son(x)}&1\end{bmatrix}\begin{bmatrix}g_......
  • 【BFS】abc351D Grid and Magnet 题解
    传送门D-GridandMagnet题意:给定h行w列的图表,其中部分位置有磁铁,人物会在磁铁四周((x+1,y),(x-1,y),(x,y+1),(x,y-1))停止,某点的自由度定义为从该点出发最多可到的方块数目可以走重复路前置例题:求连通块大小洛谷P1141思路:由自由度的定义联想到连通块的大小,从而决定用BFS......
  • ABC351
    我多久没更新这个系列了啊E把格子分成两类,每一类之间的坐标均可互相走到。然后将这里面的点都旋转\(45\)度,于是这个问题就被转换成曼哈顿距离的问题了。我们可以把\(x\)和\(y\)拆开计算。然后我们排个序,求个差分,然后对于每一个区间算贡献即可。code......
  • ABC351_F 题解
    实际上很板。考虑在\(i\)后小于\(val_i\)的数都对答案没贡献,所以我们只需要知道在\(i\)后且大于\(val_i\)的数的和以及有多少个这样的数就可以了。知道了我们要求什么,就可以一眼权值线段树。从后往前扫不断加入数,然后访问对应区间即可,当然因为值域比较大,所以还要离散化......
  • ABC351D_MagicalCookies
    MagicalCookies根据问题的描述,如果在判断同一行或同一列的所有饼干是否具有相同颜色时,选择了时间复杂度为\(\Theta(H)\)或\(\Theta(W)\)的方法,那么在每次操作1或操作2中,时间复杂度将变为\(\Theta(HW)\),因此在最坏情况下,整个计算的时间复杂度将为\(\Theta(HW(H+W))\),可......