ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AtCoder Regular Contest 120 AB题

2021-08-02 19:02:52  阅读:225  来源: 互联网

标签:AtCoder int 斜线 ll cin 120 Regular 涂色 ans


比赛链接:Here

A - Max Add

观察一下发现每次输出与两点有关,前缀和和当前位置最大值

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n; cin >> n;
    ll s = 0, t = 0, mx = INT_MIN;
    for (int i = 1, x; i <= n; ++i) {
        cin >> x;
        s += x, t += s;
        mx = max(mx, 1ll * x);
        cout << t + 1ll * i * mx << "\n";
    }
}

B - Uniformly Distributed

问的是网格里有红色,蓝色,和没涂色的格子,问有多少种方法,将没涂色的格子上色,使得,无论怎么走(题目规定只能向下,向右),使得经过的红色的数量相等。

思路:从必经之路上(斜线)入手,如给斜线上两种颜色都有,那么题目无解,如果仅有一种颜色,那该斜线仅有一种涂色方式,如果都没有,则可涂两种颜色。于是问题的答案变成了 \(2^{cnt}\) ,cnt 代表没有涂色的斜线的条数.

ARC120B

const int N = 510, mod = 998244353;
string s[N];
ll qpow(ll a, ll b) {
    ll ans = 1 ;
    for (; b; b >>= 1, a = a * a % mod);
    if (b & 1) ans = ans * a % mod;
    return ans;
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> s[i], s[i] = "@" + s[i];
    int r = 0, b = 0, cnt = 0, ans = 0;
    bool f = 0;
    for (int i = 1; i <= n; ++i) {
        int x = i, y = 1;
        r = 0, b = 0, cnt = 0;
        while (x >= 1 and y <= m) {
            if (s[x][y] == '.')  cnt++;
            if (s[x][y] == 'R')  r = 1;
            if (s[x][y] == 'B')  b = 1;
            x--, y++;
        }
        if (r == 1 and b == 1)f = 1;
        else if (r == 1 and b == 0 || r == 0 and b == 1);
        else {
            if (cnt) ans++;
        }
    }
    for (int i = 1; i <= m; ++i) {
        if (i == 1) continue;
        int x = n, y = i;
        r = 0, b = 0, cnt = 0;
        while (x >= 1 and y <= m) {
            if (s[x][y] == '.')  cnt++;
            if (s[x][y] == 'R')  r = 1;
            if (s[x][y] == 'B')  b = 1;
            x--, y++;
        }
        if (r == 1 and b == 1)f = 1;
        else if (r == 1 and b == 0 || r == 0 and b == 1);
        else {
            if (cnt) ans++;
        }
    }
    if (f) ans = 0;
    else ans = (ans + qpow(2, ans)) % mod;
    cout << ans << "\n";
}

上面代码写复杂了,看了下其他人的发现一个很简洁的写法

const int md = 998244353;
int n, m, i, j, r;
char s[505][505], c[1010];
void solve() {
    scanf("%d%d", &n, &m);
    for (i = 0; i < n; i++) {
        scanf("%s", s[i]);
        for (j = 0; j < m; j++) if (s[i][j] != '.') {
                if (c[i + j] != 0 && c[i + j] != s[i][j]) { puts("0"); return ;}
                c[i + j] = s[i][j];
            }
    }
    for (r = 1, i = 0; i <= n + m - 2; i++) if (c[i] == 0) r = (r * 2) % md;
    printf("%d\n", r);
}

标签:AtCoder,int,斜线,ll,cin,120,Regular,涂色,ans
来源: https://www.cnblogs.com/RioTian/p/15091226.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有