1. 程式人生 > 其它 >AtCoder Regular Contest 120 AB題

AtCoder Regular Contest 120 AB題

比賽連結: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 代表沒有塗色的斜線的條數.

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

The desire of his soul is the prophecy of his fate
你靈魂的慾望,是你命運的先知。