Codeforces Round #639 (Div. 2)

比赛链接:https://codeforces.com/contest/1345

A - Puzzle Pieces

题意

能否用 3 凸 1 凹的拼图拼成 n x m 的图块。

题解

观察发现只可以拼成单独的一行(列)或两行两列。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, m; cin >> n >> m;
    if (n == 1 or m == 1 or (n == 2 and m == 2)) cout << "YES" << "
";
    else cout << "NO" << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

B - Card Constructions

题意

用 $n$ 张纸牌从大到小搭纸牌屋,问可以搭的纸牌屋的数量。

题解

每层两两相顶和平铺的纸牌个数均为等差数列,求和得:

层数为 $n$ 的纸牌屋需要 $frac{n(n+1)}{2} * 2 + frac{n (n - 1)}{2}$ 张纸牌。

代码

#include <bits/stdc++.h>
using LL = long long;
using namespace std;

LL cal(LL n) {
    return n * (n + 1) + n * (n - 1) / 2;
}

void solve() {
    int n; cin >> n;
    int h = 0;
    for (int i = 1; i < INT_MAX; i++) {
        if (cal(i) > n and cal(i - 1) <= n) {
            h = i - 1;
            break;
        }
    }
    int ans = 0;
    while (n >= 2) {
        if (n >= cal(h)) ++ans, n -= cal(h);
        else --h;
    }
    cout << ans << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

C - Hilbert's Hotel

题意

有一个大小为 $n$ 的数组 $a$,将整数集 $Z$ 中的每个整数 $k$ 移至 $k + a_{k\%n}$ 的位置,问同一位置是否只有一个数。

题解

将整数集分为一个个长为 $n$ 的连续区间,如果一个连续区间能移至 $n$ 个不同的位置,其他区间可视为该区间步长为 $n$ 的平移,也因此这些不同的位置彼此间距不能为 $n$,即不存在两个新位置同余于 $n$ 。

Tips

正负数同余但会各占一个位置,可以统一转化为非负数。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        mp[((i + x) % n + n) % n]++;
    }
    for (auto i : mp) {
        if (i.second >= 2) {
            cout << "NO" << "
";
            return;
        }
    }
    cout << "YES" << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

D - Monopole Magnets

题意

有一个 n x m 的方块阵和两种单极磁铁,S 极一次可以吸引同行或列的 N 极移动一格。

同一方块内可放多个单极磁铁,要求通过放置:

  • 每个行和列都至少有一个 S 极
  • 所有白方块不可以被 N 极到达
  • 所有黑方块都可以被 N 极到达

S 极个数不限,求满足条件至少需要放置多少个 N 极。

题解

因为每行每列都需要放一个 S 极,所以一个空行(列)必须对应一个空列(行),否则该空行(列)一定可以吸引非空行(列)的 N 极。

其次两个黑方块之间不能有白方块,否则可以让一端黑方块的 S 极吸引另一端黑方块的 N 极途径二者之间的白方块。

最少的 N 极个数即黑方块的连通块个数。

代码

#include <bits/stdc++.h>
using namespace std;

const int M = 1100;
int n, m;
char MP[M][M];
bool row_white[M];
bool col_white[M];
int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

void dfs(int r, int c) {
    MP[r][c] = ' ';
    for (int i = 0; i < 4; i++) {
        int x = r + dir[i][0];
        int y = c + dir[i][1];
        if (MP[x][y] == '#') dfs(x, y);
    }
}

bool ok() {
    //预处理空行和空列
    for (int i = 1; i <= n; i++) {
        bool all_white = true;
        for (int j = 1; j <= m; j++)
            if (MP[i][j] == '#') all_white =false;
        if (all_white) row_white[i] = true;
    }
    for (int i = 1; i <= m; i++) {
        bool all_white = true;
        for (int j = 1; j <= n; j++)
            if (MP[j][i] == '#') all_white =false;
        if (all_white) col_white[i] = true;
    }
    //空行中至少有一块所在为空列
    for (int i = 1; i <= n; i++) {
        if (row_white[i]) {
            bool flag = true;
            for (int j = 1; j <= m; j++)
                if (col_white[j]) flag = false;
            if (flag) return false;
        }
    }
    //空列中至少有一块所在为空行
    for (int i = 1; i <= m; i++) {
        if (col_white[i]) {
            bool flag = true;
            for (int j = 1; j <= n; j++)
                if (row_white[j]) flag = false;
            if (flag) return false;
        }
    }
    //两个黑方块之间不能有白方块
    for (int i = 1; i <= n; i++) {
        int l = 0, r = 0;
        for (int j = 1; j <= m; j++)
            if (MP[i][j] == '#') { l = j; break; }
        for (int j = m; j >= 1; j--)
            if (MP[i][j] == '#') { r = j; break; }
        for (int j = l; j <= r; j++)
            if (MP[i][j] == '.') return false;
    }
    for (int i = 1; i <= m; i++) {
        int u = 0, d = 0;
        for (int j = 1; j <= n; j++) 
            if (MP[j][i] == '#') { u = j; break; }
        for (int j = n; j >= 1; j--)
            if (MP[j][i] == '#') { d = j; break; }
        for (int j = u; j <= d; j++) 
            if (MP[j][i] == '.') return false;
    }
    return true;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> MP[i][j];
    if (!ok()) { 
        cout << -1; 
        return 0; 
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (MP[i][j] == '#')
                dfs(i, j), ++ans;
    cout << ans;
} 

挺好的一场比赛,unrated 了有点心疼出题人...

原文地址:https://www.cnblogs.com/Kanoon/p/12840232.html