Codeforces Round #639 (Div. 2)

题目传送门

第一次尝试视频题解的方式,已将视频发在b站上面,详细可戳我
如果有什么不足之处还请大家指出。

这里说一下(C)题较为详细的证明,题目就等价为经过操作过后,不存在一个位置上面有超过(2)个点,如果有空缺那么有个位置点数必然大于(1)
如果两个点可以到达同一个位置,那么就有(displaystyle i+a_{i\% n}equiv j+a_{j\% n}(mod n)),所以我们可以将(i,j)都转化为(\% n)意义下,最后再将变化结果(\%n)看看是否重复就行。


代码:

A. Puzzle Pieces
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/6 22:37:55
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
 
void run() {
    int n, m; cin >> n >> m;
    if (n == 1 || m == 1) {
        cout << "YES" << '
';   
    } else if (n <= 2 && m <= 2) {
        cout << "YES" << '
';   
    } else cout << "NO" << '
';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T; while(T--)
    run();
    return 0;
}
B. Card Constructions
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/6 22:41:30
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 100000 + 5;
 
ll f[N], k;
 
void init() {
    for (int i = 1;; i++) {
        f[i] = f[i - 1] + 2 + 3 * (i - 1);
        if (f[i] >= (ll)1e9) {
            k = i; break;   
        }
    }
}
 
void run() {
    int n; cin >> n;
    int ans = 0;
    while(1) {
        int t = upper_bound(f + 1, f + k + 1, n) - f - 1;
        if (f[t] == n) {
            ++ans; break;
        }
        if (t <= 0) break;
        n -= f[t];
        ++ans;
    }
    cout << ans << '
';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    init();
    int T; cin >> T; while(T--)
    run();
    return 0;
}
C. Hilbert's Hotel
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/6 22:57:54
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
 
int n;
int a[N];
bool vis[N];
 
void run() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];   
    }
    for (int i = 0; i < n; i++) vis[i] = false;
    for (int i = 0; i < n; i++) {
        int x = ((i + a[i]) % n + n) % n;
        if (vis[x]) {
            cout << "NO" << '
';   
            return;
        }
        vis[x] = true;
    }
    cout << "YES" << '
';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T; while(T--)
    run();
    return 0;
}
D. Monopole Magnets
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/6 23:21:04
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e3 + 5;
 
int n, m;
char s[N][N];
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
bool vis[N][N];
int cols[N], rows[N];
 
void dfs(int x, int y) {
    if (vis[x][y]) return ;
    vis[x][y] = 1;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (s[nx][ny] == '#') dfs(nx, ny);
    }
}
 
void run() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> (s[i] + 1);
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == '#') {
                ++cols[j], ++rows[i];
            }
        }       
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (cols[j] == 0 && rows[i] == 0) {
                vis[i][j] = true;
            }
        }   
    }
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        for (int j = 1; j <= m; j++) {
            if (vis[i][j]) ++cnt;   
        }
        cnt += rows[i];
        if (cnt == 0) {
            cout << "-1" << '
';
            return;   
        }
        int all = rows[i];
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == '.' && all && all != rows[i]) {
                cout << "-1" << '
';
                return;
            }
            if (s[i][j] == '#') --all;
        }
    }
    for (int j = 1; j <= m; j++) {
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (vis[i][j]) ++cnt;
        }
        cnt += cols[j];
        if (cnt == 0) {
            cout << "-1" << '
';
            return;   
        }
        int all = cols[j];
        for (int i = 1; i <= n; i++) {
            if (s[i][j] == '.' && all && all != cols[j]) {
                cout << "-1" << '
';
                return;
            }
            if (s[i][j] == '#') --all;
        }
    }
    memset(vis, 0, sizeof(vis));
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (!vis[i][j] && s[i][j] == '#') {
                ++ans;
                dfs(i, j);
            }
        }
    }
    cout << ans << '
';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
E. Quantifier Question
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/7 0:31:29
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
 
int n, m, f;
int ans[N], vis[N], vis2[N];
vector <int> G[N], rG[N];
 
void dfs(int u) {
    vis[u] = 1;
    for (auto v : G[u]) {
        if (vis[v] == 1) f = 1;
        else if(!vis[v]) dfs(v);
    }
    vis[u] = 2;
}
 
void dfs2(int u) {
    vis2[u] = 1;
    for (auto v : rG[u]) {
        if (!vis2[v]) dfs2(v);   
    }
}
 
void run() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        rG[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) dfs(i);
    }
    if (f) {
        cout << -1 << '
';
        return;
    }
    memset(ans, -1, sizeof(ans));
    memset(vis, 0, sizeof(vis));
    int res = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis[i] && !vis2[i]) {
            ans[i] = 1;
            ++res;
        } else {
            ans[i] = 0;
        }
        if (!vis[i]) dfs(i);
        if (!vis2[i]) dfs2(i);   
    }
    cout << res << '
';
    for (int i = 1; i <= n; i++) {
        if (ans[i]) cout << "A";
        else cout << "E";
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
F - Résumé Review
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/7 21:30:21
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;

ll n, k;
int a[N], b[N];

bool chk(ll A) {
    ll s = 0;
    for (int i = 1; i <= n; i++) {
        int l = 0, r = a[i] + 1, mid;
        while (l < r) {
            mid = (l + r) >> 1;
            if (a[i] - 3ll * mid * (mid - 1) - 1 >= A) l = mid + 1;
            else r = mid;
        }   
        b[i] = l - 1;
        s += b[i];
    }
    return s >= k;
}

void run() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    ll l = -8e18, r = 8e18, mid;
    while (l < r) {
        mid = (l + r) >> 1;
        if (chk(mid)) {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    ll A = l - 1, R = 0;
    chk(A);
    for (int i = 1; i <= n; i++) R += b[i];
    R -= k;
    for (int i = 1; i <= n && R; i++) {
        if (b[i] && a[i] - 3ll * b[i] * (b[i] - 1) - 1 == A) {
            --b[i], --R;
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << b[i] << " 
"[i == n];   
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
原文地址:https://www.cnblogs.com/heyuhhh/p/12842379.html