Codeforces Round #593 (Div. 2)

Contest Info


Practice Link

Solved A B C D E F
5/6 THE THE THE THE Ø -
  • O through the game
  • Ø After the game by
  • ! I tried but failed
  • - No attempt

Solutions


A. Stones

Sign.

Code:


view code

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e5 + 10;
int a, b, c; 
void run() {
    cin >> a >> b >> c;
    int res = 0;
    int x = min(b, c / 2);
    res += 3 * x;
    b -= x;
    res += min(a, b / 2) * 3;
    pt(res);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed << setprecision(20);
    int _T; cin >> _T;
    while (_T--) run();
    return 0;
}

B. Alice and the List of Presents

The meaning of problems:
there are \ (n-\) number of species, there are \ (m \) sets need to \ (m \) sets put some number, such that at least each of the number appeared to be in a set.

Ideas:
Consider consideration for each number, a number in the \ (m \) appears in sets or the number of programs does not appear \ (2 ^ m \) , then subtract all without appearing to.

Code:


view code

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr ll mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e5 + 10;
ll n, m; 
void run() {
    pt(qpow((qpow(2, m) - 1 + mod) % mod, n));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed << setprecision(20);
    while (cin >> n >> m) run();
    return 0;
}

C. Labs

Meaning of the questions:
There \ (n ^ 2 \) laboratories, large energy label to label small water, to remove them now grouped laboratory within the group can circulate to each other, the flow is defined between the group and the group \ (F (X-, the Y) \) , indicates the number of \ ((i, j) \ ) such that \ (i \ in X, j \ in Y \) and \ (I> J \) .
To now \ (n ^ 2 \) labs into \ (n-\) group, such that the smallest \ (f (X, Y) \) maximum.

Ideas:
like this:

1 6 7
2 5 8
3 4 9

Code:


view code

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e2 + 10;
int n, a[N][N]; 
void run() {
    for (int j = 1; j <= n; ++j) {
        int id = (j - 1) * n;
        if (j & 1) {
            for (int i = 1; i <= n; ++i)
                a[i][j] = ++id;
        } else {
            for (int i = n; i >= 1; --i) {
                a[i][j] = ++id;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            cout << a[i][j] << " \n"[j == n];       
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed << setprecision(20);
    while (cin >> n) run();
    return 0;
}

D. Alice and the Doll

The meaning of problems:
in a \ (n \ cdot m \) rectangular, there \ (K \) th obstacle, from now \ ((1, 1) \) starting rotation at each point only the right once, and each point can only be accessed once asked if he could finish all non-barrier access nodes.

Thinking:
path to access is certainly serpentine, simulation can be.

Code:


view code

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e5 + 10;
int n, m, k;
set <int> row[N], col[N]; 
bool del(int up, int down, int left, int right) {
    for (int i = up; i <= down; ++i) {
        for (int j = left; j <= right; ++j) {
            if (row[i].find(j) == row[i].end() || col[j].find(i) == col[j].end()) {
                return false;
            } else {
                row[i].erase(j);
                col[j].erase(i);
                --k; 
            }
        }
    }
    return true;
}
//dir
//0 right
//1 down
//2 left
//3 up
void run() {
    for (int i = 1; i <= n; ++i) row[i].clear();
    for (int i = 1; i <= m; ++i) col[i].clear();
    for (int i = 1, x, y; i <= k; ++i) {
        cin >> x >> y;
        row[x].insert(y);
        col[y].insert(x);
    }
    if (!k) return pt("Yes");
    int up = 1, down = n, left = 1, right = m; 
    int x = 1, y = 1, dir = 0;
    bool F = 0;
    while (k) {
    //  dbg(up, down, left, right, dir, x, y); 
        if (dir == 0) { // right
            auto it = row[x].begin(); 
            if (it == row[x].end()) { 
                y = right; 
            } else {
                int pos = *it; 
                if (!del(up, down, pos, right)) {
                    return pt("No");
                }
                right = pos - 1; 
                y = pos - 1; 
            }
            left += F;
        } else if (dir == 1) { // down
            auto it = col[y].begin();
            if (it == col[y].end()) { 
                x = down;
            } else {
                int pos = *it;
                if (!del(pos, down, left, right)) {
                    return pt("No");
                }
                down = pos - 1;
                x = pos - 1;
            }
            up += F;
        } else if (dir == 2) { // left
            auto it = row[x].end();
            if (it != row[x].begin()) {
                --it;
                int pos = *it;
                if (!del(up, down, left, pos)) {
                    return pt("No");
                }
                left = pos + 1;
                y = pos + 1;
            } else {
                y = left;
            }
            right -= F;
        } else { // up
            auto it = col[y].end();
            if (it != col[y].begin()) {
                --it;
                int pos = *it;
                if (!del(up, pos, left, right)) {
                    return pt("No");
                }
                up = pos + 1;
                x = pos + 1;
            } else {
                x = up;
            }
            down -= F;
        }
        dir = (dir + 1) % 4;
        F = 1;
    }
    pt("Yes");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed << setprecision(20);
    while (cin >> n >> m >> k) run();
    return 0;
}

E. Alice and the Unfair Game

The meaning of problems:
In a one-dimensional axis length \ (n-\) , at the \ (I \) of the second \ (a_i \) positions have monster, defines a binary group \ ((X, y) \) represents the beginning of the \ (x \) departure, the last to stay in the \ (y \) is feasible.
Q. How many such tuple feasible.

Ideas:
guess for a starting point \ (x \) , it's possible \ (y \) is a period of range, then we just need to find the furthest \ (y \) can be.
We put it on the two-dimensional coordinate system is a time dimension, a dimension that position.
From the beginning \ ((0, x) \ ) starting to go to \ ((m, the y-) \) , and only on the right, right, bottom right away and can not come to an obstacle.
And we only need to care can go the farthest place, then to the upper right, for example, can go on the right upper right, or right.
Then find the line with a slope of about greedy \ (dp \) can be.

Code:


view code

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e6 + 10, OFFSET = 2e5;
int n, m, a[N], l[N], r[N], f[N], last[N];
//y - x
void gaol() {
    memset(last, -1, sizeof last);
    for (int i = m; i >= 1; --i) {
        int p = a[i] + 1 + i;
        if (last[p] == -1) f[i] = max(1, a[i] - (m - i)); 
        else f[i] = f[last[p]];
        last[a[i] + i] = i;
    }
    for (int i = 1; i <= n; ++i) {
        int p = i;
//      dbg(i, p, last[p]);
        if (last[p] == -1) l[i] = max(1, i - (m + 1));
        else l[i] = f[last[p]];
    }
}
void gaor() {
    memset(last, -1, sizeof last);
    for (int i = m; i >= 1; --i) {
        int p = a[i] - 1 - i + OFFSET; 
        if (last[p] == -1) f[i] = min(n, a[i] + m - i); 
        else f[i] = f[last[p]];
        last[a[i] - i + OFFSET] = i;
    }
    for (int i = 1; i <= n; ++i) {
        int p = i + OFFSET;
        if (last[p] == -1) r[i] = min(n, i + m + 1);
        else r[i] = f[last[p]];
    }
}
void run() {
    for (int i = 1; i <= m; ++i) a[i] = rd();
    if (n == 1) return pt(0);
    gaol();
    gaor();
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
    //  dbg(i, l[i], r[i]);
        ans += r[i] - l[i] + 1;
    }
    pt(ans);    
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed << setprecision(20);
    while (cin >> n >> m) run();
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dup4/p/11717716.html