码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #639 (Div. 2)

时间:2020-05-07 13:11:34      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:space   memset   VID   ack   using   inline   contest   ret   err   

题目传送门

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

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


代码:

A
/*
 * 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" << ‘\n‘;   
    } else if (n <= 2 && m <= 2) {
        cout << "YES" << ‘\n‘;   
    } else cout << "NO" << ‘\n‘;
}
 
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
/*
 * 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 << ‘\n‘;
}
 
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
/*
 * 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" << ‘\n‘;   
            return;
        }
        vis[x] = true;
    }
    cout << "YES" << ‘\n‘;
}
 
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
/*
 * 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" << ‘\n‘;
            return;   
        }
        int all = rows[i];
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == ‘.‘ && all && all != rows[i]) {
                cout << "-1" << ‘\n‘;
                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" << ‘\n‘;
            return;   
        }
        int all = cols[j];
        for (int i = 1; i <= n; i++) {
            if (s[i][j] == ‘.‘ && all && all != cols[j]) {
                cout << "-1" << ‘\n‘;
                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 << ‘\n‘;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
E
/*
 * 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 << ‘\n‘;
        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 << ‘\n‘;
    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;
}

Codeforces Round #639 (Div. 2)

标签:space   memset   VID   ack   using   inline   contest   ret   err   

原文地址:https://www.cnblogs.com/heyuhhh/p/12842379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!