AtCoder ABC176

前面几个题都比较简单,但是F题很考验基本功
C - Step
签到题,贪心维护一个当前的最高值

D - Wizard in Maze
最短路的做法,最小堆维护

/*
 * @Author: C.D.
 * @Date: 2023-12-12 08:14:36
 * @LastEditors: C.D.
 * @LastEditTime: 2023-12-12 11:00:11
 * @FilePath: atcoder.cpp
 *
 * Copyright (c) 2023 by C.D./tongwoo.cn, All Rights Reserved.
 */

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int h, w;
int ch, cw, dh, dw;
char mat[1002][1002];
int dist[1002][1002];


struct Grid {
    int step;
    int r, c;
    Grid(int _step, int _r, int _c) {
        step = _step, r = _r, c = _c;
    }
    bool operator <(const Grid& other) const {
        return this->step < other.step;
    }
    bool operator >(const Grid& other) const {
        return this->step > other.step;
    }
};


priority_queue<Grid, vector<Grid>, greater<Grid>> qu;
int dirs[4][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} };


int main() {
    //freopen("in.txt", "r", stdin);
    scanf("%d%d", &h, &w);
    scanf("%d%d%d%d", &ch, &cw, &dh, &dw);
    ch--, cw--;
    dh--, dw--;
    for (int i = 0; i < h; ++i) {
        scanf("%s", mat[i]);
    }
    memset(dist, 0x33, sizeof(dist));
    dist[ch][cw] = 0;
    Grid grid(0, ch, cw);
    qu.push(grid);
    //qu.push(Grid(1, 0, 0));
    while (!qu.empty()) {
        Grid top = qu.top();
        qu.pop();
        int r = top.r, c = top.c;
        int step = top.step;
        if (r == dh && c == dw) {
            break;
        }

        for (int i = 0; i < 4; i++) {
            int nr = r + dirs[i][0], nc = c + dirs[i][1];
            if (nr >= 0 && nr < h && nc >= 0 && nc < w && mat[nr][nc] == '.') {
                if (dist[nr][nc] > step) {
                    dist[nr][nc] = step;
                    qu.push(Grid(step, nr, nc));
                }
            }
        }
        for (int i = -2; i <= 2; ++i) {
            for (int j = -2; j <= 2; ++j) {
                int nr = r + i, nc = c + j;
                if (nr >= 0 && nr < h && nc >= 0 && nc < w && mat[nr][nc] == '.') {
                    if (dist[nr][nc] > step + 1) {
                        dist[nr][nc] = step + 1;
                        qu.push(Grid(step + 1, nr, nc));
                    }
                }
            }
        }
    }
    int ans = dist[dh][dw];
    if (ans > 1 << 20) {
        ans = -1;
    }
    printf("%d\n", ans);
    return 0;
}

E - Bomber
本题和F题有异曲同工之处
用一个map来记录点
因为答案肯定在maxh和maxw的那些行列中,因此遍历一下行h和列w,如果map[h][w] == 1则跳过,不然就可以直接确定有一个点的答案是maxh+maxw。因为点数量<300000,所以最多遍历300000次,可以做。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int H, W, M;
map<pii, bool> mark;
int h[300005];
int w[300005];


int main() {
    //freopen("in.txt", "r", stdin);
    scanf("%d%d%d", &H, &W, &M);
    for (int i = 0; i < M; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        mark[{a, b}] = 1;
        h[a] ++, w[b] ++;
    }
    
    int maxh = -1, maxw = -1;
    for (int i = 1; i <= H; ++i)
        maxh = max(maxh, h[i]);
    for (int i = 1; i <= W; ++i)
        maxw = max(maxw, w[i]);
    vector<int> hs, ws;
    for (int i = 1; i <= H; ++i) {
        if (h[i] == maxh)
            hs.push_back(i);
    }
    for (int i = 1; i <= W; ++i) {
        if (w[i] == maxw)
            ws.push_back(i);
    }
    for (auto a : hs) {
        for (auto b : ws) {
            if (mark[{a, b}]) {
                continue;
            }
            else {
                int ans = maxh + maxw;
                printf("%d\n", ans);
                return 0;
            }

        }
    }
    printf("%d\n", maxh + maxw - 1);
    return 0;
}

F - Brave CHAIN
首先要确定状态
设dp[i][x][y]是第i次结束后留下牌面为xy的最大得分
看起来很大的状态空间(n*n)其实真正发生转移的不会很多
先考虑得分的转移情况
设每次拿到新的牌的排列情况是:
1.aaa
2.aab
3.abb
4.abc
其中2与3本质相同
我们考虑1
显然可以直接拿走加一分
2 aab
上一轮留下两张牌为ax的情况下,
可以拿走aaa,转移为bx
在上一轮留下牌为bb的情况下,可以拿走bbb,转移为aa
3 abc
唯一可以得分的情况是上一轮留下aa bb cc

然后考虑不得分的转移情况
1.不换牌 不用改变
2.换一张牌
设拿到三张牌abc,前面的状态是xy,
新的状态是ax
转移的时候我们可以遍历剩下的一张牌i,那么 d p [ x ] [ a ] = max ⁡ i ∈ [ 1 , n ] ( d p [ x ] [ i ] ) dp[x][a] = \max_{i\in [1,n]}(dp[x][i]) dp[x][a]=maxi[1,n](dp[x][i])
为了在 O ( n ) O(n) O(n)时间内完成,加入一个维护数组 m x mx mx,维护一行/一列的最大值
3.换两张牌
方法是相似的,现在需要转移两张牌
设新的状态是ab
转移的时候我们需要遍历所有的牌dp[i][j]
那么维护一个最大值就可以

需要注意的是滚动空间。
普通的memcpy是过不了的,因此需要记录一下每次发生update的点对
然后在每次循环结束的时候更新下一个空间

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


int n;
int dp[2][2002][2002];
int mx[2002];
int lmx[2002];
int mxy, lmxy;      // single point (x, y)
int a[6006];
set<pii> turn;      // 记录更新点对


void update(int x, int y, int cur) {
    turn.insert({ x, y });
    mx[x] = max(mx[x], dp[cur][x][y]);
    if (x != y) {
        mx[y] = max(mx[y], dp[cur][x][y]);
    }
    mxy = max(mxy, dp[cur][x][y]);
}


void all_update(int cur) {
    int lst = 1 ^ cur;
    for (auto it : turn) {
        int x = it.first, y = it.second;
        dp[lst][x][y] = dp[cur][x][y];
        dp[lst][y][x] = dp[cur][y][x];
    }
    turn.clear();
    memcpy(lmx, mx, sizeof(mx));
    lmxy = mxy;
}


int main() {
    //freopen("in.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 0; i < n * 3; ++i) {
        scanf("%d", &a[i]);
        a[i] --;
    }
    int upd = 0;
    int cur = 2;
    int x = a[0], y = a[1];
    int p = 0, q = 1;

    memset(dp, 0xa3, sizeof(dp));
    memset(mx, 0xa3, sizeof(mx));
    dp[p][x][y] = dp[p][y][x] = 0;
    update(x, y, p);
    memcpy(lmx, mx, sizeof(lmx));


    while (cur + 2 < n * 3) {
        p = p ^ 1;
        q = 1 - p;
        memset(mx, 0xa3, sizeof(mx));
        mxy = int(-1e8);
        vi temp{ a[cur], a[cur + 1], a[cur + 2] };
        sort(temp.begin(), temp.end());
        if (temp[0] == temp[1] && temp[1] == temp[2]) {       // xxx
            upd += 1;
            cur += 3;
            continue;
        }
        else if (temp[0] == temp[1]) {      // xxy  
            int x = temp[0], y = temp[2];
            for (int i = 0; i < n; ++i) {
                dp[p][i][y] = dp[p][y][i] = max(dp[p][y][i], dp[q][x][i] + 1);
                update(i, y, p);
            }
            dp[p][x][x] = max(dp[p][x][x], dp[q][y][y] + 1);
            update(x, x, p);
        }
        else if (temp[1] == temp[2]) {      // xyy
            int x = temp[0], y = temp[1];
            for (int i = 0; i < n; ++i) {
                dp[p][i][x] = dp[p][x][i] = max(dp[p][i][x], dp[q][y][i] + 1);
                update(i, x, p);
            }
            dp[p][y][y] = max(dp[p][y][y], dp[q][x][x] + 1);
            update(y, y, p);
        }
        else {  // xyz
            int x = temp[0], y = temp[1], z = temp[2];
            dp[p][y][z] = dp[p][z][y] = max(dp[p][y][z], dp[q][x][x] + 1);
            dp[p][x][y] = dp[p][y][x] = max(dp[p][x][y], dp[q][z][z] + 1);
            dp[p][x][z] = dp[p][z][x] = max(dp[p][x][z], dp[q][y][y] + 1);
            update(x, y, p);
            update(y, z, p);
            update(z, x, p);
        }
        int x = temp[0], y = temp[1], z = temp[2];
        // ij -> ij do nothing
        
        // ij -> ix  .. max(i, k) // mx[i]
        for (int i = 0; i < n; ++i) {
            dp[p][x][i] = dp[p][i][x] = max(dp[p][i][x], lmx[i]);
            update(i, x, p);
            dp[p][y][i] = dp[p][i][y] = max(dp[p][i][y], lmx[i]);
            update(i, y, p);
            dp[p][z][i] = dp[p][i][z] = max(dp[p][i][z], lmx[i]);
            update(i, z, p);
        }
        // ij -> xy ..  max(k, l) // mxy
        dp[p][x][y] = dp[p][y][x] = max(dp[p][x][y], lmxy);
        dp[p][z][y] = dp[p][y][z] = max(dp[p][z][y], lmxy);
        dp[p][x][z] = dp[p][z][x] = max(dp[p][x][z], lmxy);
        update(x, y, p); update(y, z, p); update(x, z, p);

        all_update(p);
        cur += 3;
    }
    x = a[n * 3 - 1];
    int ans = upd + max(dp[p][x][x] + 1, lmxy);
    printf("%d\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: atcoder beginner contest 235 是一场由 AtCoder 组织的初学者比赛,旨在为初学者提供一个锻炼自己编程能力的平台。比赛通常包括多个问题,参赛者需要在规定时间内解决这些问题。比赛难度逐渐增加,从而帮助参赛者提高自己的编程技能。 ### 回答2: ATCoder Beginner Contest 235(简称ABC 235)是一项由ATCoder举办的编程竞赛。该比赛旨在为初学者提供一个机会展示他们的编程技巧和解决问题的能力。 ABC 235通常由4个问题组成,题目难度递增。参赛者需要在规定的时间内使用编程语言解决这些问题。这些问题通常涵盖了各种编程相关的主题,例如数学问题、字符串处理、排序和搜索算法等。 在比赛开始前,参赛者将获得一份题目说明文档和输入样例。他们需要根据题目要求编写程序,处理给定的输入数据,并生成相应的输出。比赛时间一般为2-3小时,参赛者需要尽可能快速且准确地解决问题。 评判将根据参赛者的程序输出与预期结果的一致性进行。参赛者可以在比赛过程中提交多次解答,但只有第一次正确答案会被记入最终的成绩。 ABC 235不仅提供了一个竞赛平台,还鼓励参赛者通过讨论和分享解题思路来学习和提高。在比赛结束后,ATCoder将提供详细的解题分析和解题报告,帮助参赛者了解每个题目的最佳解决方法,并提供参考答案和示例代码。 通过参加ATCoder Beginner Contest 235,参赛者可以提升他们的编程技能,锻炼逻辑思维能力,并与全球的编程爱好者交流。无论是初学者还是有经验的编程者,ABC 235都是一个很好的学习和挑战的机会。 ### 回答3: AtCoder Beginner Contest 235 是一个在 AtCoder 上的初级比赛。该比赛通常会吸引很多新手程序员参加。它由 AtCoder 组织主办,旨在帮助新手提高编程技能以及在竞赛中锻炼自己。 比赛的题目难度由易到难,共有四个问题。通常,第一个问题是一个简单的数学问题,要求解决一个简单的算术运算。第二个问题可能是一个字符串操作问题,需要对给定的字符串进行处理。第三个问题可能是一个动态规划或贪心算法问题,需要细心分析问题,找出最优解。最后一个问题通常是一个较难的图论或组合问题,需要一些高级算法来解决。 参赛选手在比赛开始后有一定的时间限制来解决这些问题。他们可以使用自己熟悉的编程语言来实现解决方案。然后他们将自己的程序提交到 AtCoder 的在线评测系统中进行评测。评测结果会即时显示,并将参赛选手的成绩排名和讨论解答过程的视频分享给其他选手。 参加 AtCoder Beginner Contest 235 对于新手来说是一个很好的机会。通过解决这些问题,他们可以练习编程技巧,提高解决问题的能力。比赛结束后,他们还可以看到其他选手的解答,学习他们的思路和方法。同时,比赛的排名和奖励也是一种鼓励和激励新手继续努力学习的方式。 总之,AtCoder Beginner Contest 235 是一个对于新手非常友好的比赛,它提供了一个锻炼和展示编程技能的平台。无论是对于新手还是更有经验的选手,参加这样的比赛都是一个宝贵的机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值