AtCoder Beginner Contest 167

题目传送门

还是视频题解,但感觉有点没说清楚的地方。。

\(F\)感觉有点没有讲清楚,最后\(b_i<0\)的情况,我们按照\(a_i+b_j<a_j+b_i\)进行排序。考虑对\(i,j\)两个位置安排顺序,并且考虑先放\(i\)再放\(j\),先放\(j\)再放\(i\)两种情况,我们假设两个首先都能成功放上,那么就需要使得\(a_i+b_j\)尽可能大,此时后面就越有可能放上去。
如果第一个不能放上呢,那么之后的顺序无论怎样,都是不合法的,最后就类似于这样的:11110000,1表示合法,0表示不合法,我们前面已经放上了一些,从第一个0开始,我们顺序其实已经无所谓了。所以我们不用考虑不能放上的情况。
那么把能放上的情况考虑就行。


代码如下:

A - Registration
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 20:00:37
 */
#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() {
    string s, t; cin >> s >> t;
    int n = s.length(), m = t.length();
    if (m == n + 1 && t.substr(0, n) == s) {
        cout << "Yes" << '\n';
    } else cout << "No" << '\n';
}

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

B - Easy Linear Programming /
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 20:03:01
 */
#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 A, B, C, k; cin >> A >> B >> C >> k;
    int ans = min(A, k);
    k -= ans;
    if (k) {
        k -= B;
    }
    if (k > 0) {
        ans -= min(C, k);
    }
    cout << ans << '\n';
}

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

C - Skill Up
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 20:06:28
 */
#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, x; cin >> n >> m >> x;
    vector <vector <int>> a(n);
    for (int i = 0; i < n; i++) {
        int k; cin >> k;
        a[i].resize(m + 1);
        a[i][m] = k;
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    int ans = INF;
    for (int i = 0; i < 1 << n; i++) {
        vector <int> cnt(m, 0);
        int res = 0;
        for (int j = 0; j < n; j++) if (i >> j & 1) {
            res += a[j][m];
            for (int k = 0; k < m; k++) {
                cnt[k] += a[j][k];
            }
        }
        bool f = true;
        for (auto it : cnt) {
            if (it < x) f = false;
        }
        if (f) {
            ans = min(ans, res);
        }
    }
    if (ans == INF) ans = -1;
    cout << ans << '\n';
}

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

D - Teleporter
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 20:13:08
 */
#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

void run() {
    int n; ll k; 
    cin >> n >> k;
    vector <int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i]; --a[i];
    }
    vector <int> vis(n);
    vector <int> sta, cyc;
    int x = 0;
    while (1) {
        vis[x] = 1;
        sta.push_back(x);
        x = a[x];
        if (vis[x]) {
            while (1) {
                cyc.push_back(sta.back());
                int t = sta.back();
                sta.pop_back();
                if (t == x) {
                    break;
                }
            }
            break;
        }
    }
    reverse(all(cyc));
    if (k < sz(sta)) {
        cout << sta[k] + 1 << '\n';   
    } else {
        k -= sz(sta);
        k %= sz(cyc);
        cout << cyc[k] + 1 << '\n';
    }
}

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

E - Colorful Blocks
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 20:57:40
 */
#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, MOD = 998244353;

int qpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;   
}
int fac[N], inv[N];

void run() {
    int n, m, k;
    cin >> n >> m >> k;
    fac[0] = 1;
    for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
    inv[n] = qpow(fac[n], MOD - 2);
    for (int i = n - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
    int ans = 0;
    for (int t = n - k; t <= n; t++) {
        //x_1+x_2+..x_t = n
        int res = 1ll * m * qpow(m - 1, t - 1) % MOD;
        res = 1ll * res * fac[n - 1] % MOD * inv[t - 1] % MOD * inv[n - t] % MOD;
        ans = (ans + res) % MOD;
    }
    cout << ans << '\n';
}

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

F - Bracket Sequencing
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/10 21:13:22
 */
#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 = 1e6 + 5;

void run() {
    int n; cin >> n;
    vector <pii> z(n), f(n), o(n);
    for (int i = 1; i <= n; i++) {
        string s; cin >> s;
        int k = s.length();
        int x = 0, Min = INF;
        for (int j = 0; j < k; j++) {
            if (s[j] == '(') ++x;
            else --x;
            Min = min(Min, x);
        }
        if (x > 0) z.push_back(MP(Min, x));
        else if (x == 0) o.push_back(MP(Min, x));
        else f.push_back(MP(Min, x));
    }
    sort(all(z), [&] (pii A, pii B) {
        return A.fi > B.fi;
    });
    sort(all(f), [&] (pii A, pii B) {
        return A.se + B.fi > A.fi + B.se;
    });
    int tot = 0;
    for (int i = 0; i < sz(z); i++) {
        pii now = z[i];
        if (tot + now.fi >= 0) {
            tot += now.se;   
        } else {
            cout << "No" << '\n';
            return;
        }
    }
    for (int i = 0; i < sz(o); i++) {
        pii now = o[i];
        if (tot + now.fi < 0) {
            cout << "No" << '\n';
            return;
        }
    }
    for (int i = 0; i < sz(f); i++) {
        pii now = f[i];
        if (tot + now.fi >= 0) {
            tot += now.se;   
        } else {
            cout << "No" << '\n';
            return;
        } 
    }
    if (tot != 0) cout << "No" << '\n';
    else cout << "Yes" << '\n';
}

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

posted @ 2020-05-11 09:05  heyuhhh  阅读(386)  评论(0编辑  收藏  举报