1. 程式人生 > 其它 >Educational Codeforces Round 114 (Rated for Div. 2) 題解

Educational Codeforces Round 114 (Rated for Div. 2) 題解

旅行傳送門

A. Regular Bracket Sequences

題意:給你一個整數 \(n\) ,構造並列印長度為 \(2n\)\(n\) 個不同的合法括號序列。

題目分析:模擬,不妨設最初的括號序列為 \(\underbrace{(((}_{n} \cdots \underbrace{)))}_{n}\) ,每次從中取出一對合法括號放外邊即可。

AC程式碼

#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); i++)
#define down(i, x, y) for (register int i = (x); i >= (y); i--)

char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

void solve()
{
    int n = read();
    down(k, n, 1)
    {
        rep(i, 1, k) printf("(");
        rep(i, 1, k) printf(")");
        rep(j, 1, n - k) printf("()");
        puts("");
    }
}

int main(int argc, char const *argv[])
{
    int T = read();
    while (T--)
        solve();
    return 0;
}

B. Combinatorics Homework

題意:給你四個整數值 \(a\)\(b\)\(c\)\(m\)

判斷是否存在包含以下內容的字串:

  • \(a\) 個字母 \(A\)
  • \(b\) 個字母 \(B\)
  • \(c\) 個字母 \(C\)
  • 正好含有 \(m\) 對相鄰的相等字母(即 \(s[i] = s[i+1]\) )。

題目分析:不妨假設 \(a \leq b \leq c\) ,先考慮相鄰相等字母的上下限:

  • 上限:\(\underbrace{AAA}_{a} \cdots \underbrace{BBBB}_{b} \cdots \underbrace{CCCCC}_{c}\)
    ,即 \((a-1)+(b-1)+(c-1)\)
  • 下限:\(\underbrace{CACA}_{a個C} \cdots \underbrace{CBCBCB}_{b個C} \cdots CCCCC\) ,即 \(c - (a+b) - 1\)

可以證明,若 \(min \leq m \leq max\) ,則這樣的序列一定存在。

  • \(min +1\)\(\underbrace{ACACA \cdots CA}_{a-1個C} \underbrace{CBCBCB}_{b個C} \cdots CCCCC+C\)
  • \(min +2\)\(\underbrace{CACA \cdots CAA}_{a-1個C} \underbrace{CBCBCB}_{b個C} \cdots CCCCC+C\)

\(min\) 的基礎上每多出一對鄰相等字母,就把字串的頭字母放到該字母最後一次出現的位置之後。

AC程式碼

#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); i++)
#define down(i, x, y) for (register int i = (x); i >= (y); i--)

char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

bool solve()
{
    int a = read(), b = read(), c = read(), m = read();
    if (c < a)
        std::swap(a, c);
    if (c < b)
        std::swap(b, c);
    int mx = a + b + c - 3, mn = std::max(c - (a + b) - 1, 0);
    return (mn <= m && m <= mx) ? true : false;
}

int main(int argc, char const *argv[])
{
    int T = read();
    while (T--)
        puts(solve() ? "YES" : "NO");
    return 0;
}

C. Slay the Dragon

題意很久很久以前,巨龍突然出現

你有一支含 \(n\) 位勇者的小隊,現在有 \(m\) 條惡龍,第 \(i\) 條的防禦為 \(x_i\) ,攻擊力為 \(y_i\) 。對每條龍,你可以選出一名能力值 \(a_i \geq x_i\) 的勇者誅戮惡龍,其餘勇者留下來防守,且防守的勇者們能力值總和 \(sum \geq y_i\)

同時,你可以花費 \(1\)\(cost\) 將任意勇者的能力值提升 \(1\) 點,此操作可以進行任意次。

問擊敗第 \(i\) 條龍的最小花費是多少(對戰每條龍時所有勇者的能力值重置)。

題目分析:採取貪心策略,找到序列中首次出現的 \(\geq\)\(\leq x_i\) 的值 \(a_i\) ,然後計算相應花費輸出較小的即可。

AC程式碼

#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); i++)
#define down(i, x, y) for (register int i = (x); i >= (y); i--)
using ll = long long;
using namespace std;

char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline bool cmp(ll a, ll b) { return a > b; }

int main(int argc, char const *argv[])
{
    int n = read();
    ll sum = 0;
    vector<ll> a(n + 1), b(n + 1);
    rep(i, 1, n) sum += (a[i] = b[i] = read());
    sort(a.begin() + 1, a.begin() + n + 1);
    sort(b.begin() + 1, b.begin() + n + 1, cmp);
    int m = read();
    while (m--)
    {
        ll x = read(), y = read();
        int pos1 = lower_bound(a.begin() + 1, a.begin() + n + 1, x) - a.begin();
        int pos2 = lower_bound(b.begin() + 1, b.begin() + n + 1, x, greater<ll>()) - b.begin();
        if (pos1 > n)
            pos1 = n;
        if (pos2 > n)
            pos2 = n;
        ll ans1 = 0, ans2 = 0;
        if (x > a[pos1])
            ans1 += x - a[pos1];
        if (y > sum - a[pos1])
            ans1 += y - (sum - a[pos1]);
        if (x > b[pos2])
            ans2 += x - b[pos2];
        if (y > sum - b[pos2])
            ans2 += y - (sum - b[pos2]);
        printf("%lld\n", min(ans1, ans2));
    }
    return 0;
}

D. The Strongest Build

題意:給你 \(n\) 個裝備槽,每個裝備槽有 \(c\) 件裝備可以挑選,每件裝備的屬性值為 \(a_{i,j}\) ,現有 \(m\) 種不合法的方案數,求在此條件下使得屬性值最大的組合方案。

題目分析:一開始用 \(dfs\) 暴搜結果 \(MLE\) 了。這裡給出一種貪心的策略,優先用更好的裝備,第 \(i\) 個裝備槽只考慮能使方案合法的最好的第 \(j\) 個裝備,每次從優先佇列中取出當前最優方案,如果這個方案已經被 \(ban\) 了,就將其分成 \(n\) 個後繼方案(一個方案的後繼就是對於當前組合裡的某個槽,用剛好差一檔的裝備換上去),但分出的後繼方案可能會有重複的,因此我們選擇用 \(set\) 去重。由於優先佇列採取的是大根堆,所以這樣的做法一定能得到最優解。

AC程式碼

#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i < (y); i++)
#define down(i, x, y) for (register int i = (x); i > (y); i--)
#define piv std::pair<int, std::vector<int>>

char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

std::vector<int> v, g;
std::map<std::vector<int>, int> mp;
std::priority_queue<piv> q;
std::vector<std::vector<int>> f;
std::set<std::vector<int>> s;

int main(int argc, char const *argv[])
{
    int n = read(), sum = 0;
    rep(i, 0, n)
    {
        int c = read();
        v.push_back(c);
        g.clear();
        rep(j, 0, c)
        {
            int k = read();
            g.push_back(k);
        }
        f.push_back(g);
        sum += g[c - 1];
    }
    q.push(std::make_pair(sum, v));
    int m = read();
    rep(i, 0, m)
    {
        g.clear();
        rep(j, 0, n)
        {
            int k = read();
            g.push_back(k);
        }
        ++mp[g];
    }
    while (!q.empty())
    {
        piv ans = q.top();
        q.pop();
        sum = ans.first;
        g = ans.second;
        if (mp[g])
        {
            rep(i, 0, n)
            {
                if (g[i] <= 1)
                    continue;
                int cur = f[i][g[i] - 1];
                --g[i];
                int nxt = f[i][g[i] - 1];
                if (!s.count(g))
                {
                    q.push(std::make_pair(sum - cur + nxt, g));
                    s.insert(g);
                }
                ++g[i];
            }
            continue;
        }
        for (auto x : g)
            printf("%d ", x);
        puts("");
        break;
    }
    return 0;
}

// 關於dfs:它死了
// #include <bits/stdc++.h>
// #define rep(i, x, y) for (register int i = (x); i <= (y); i++)
// #define down(i, x, y) for (register int i = (x); i >= (y); i--)
// #define pii pair<int, int>
// using namespace std;

// char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
// inline int read()
// {
//     int x = 0, f = 1;
//     char ch = getchar();
//     while (!isdigit(ch))
//     {
//         if (ch == '-')
//             f = -1;
//         ch = getchar();
//     }
//     while (isdigit(ch))
//     {
//         x = x * 10 + ch - '0';
//         ch = getchar();
//     }
//     return x * f;
// }
// inline char qrc()
// {
//     char c;
//     while (!isdigit(c = getchar()))
//         ;
//     return c;
// }

// int n, ans;
// map<vector<int>, int> mp;
// vector<int> output;
// vector<pii> a[11];

// void dfs(int id, vector<int> v)
// {
//     if (v.size() == n)
//     {
//         if (mp[v])
//             return;
//         int ans = 0;
//         rep(i, 1, n)
//             ans += a[i][v[i - 1] - 1].second;
//         if (ans > ans)
//             ans = ans, output = v;
//         return;
//     }
//     for (auto x : a[id])
//     {
//         v.push_back(x.first);
//         dfs(id + 1, v);
//         v.pop_back();
//     }
// }

// int main(int argc, char const *argv[])
// {
//     n = read();
//     rep(i, 1, n)
//     {
//         int c = read();
//         rep(j, 1, c)
//         {
//             int k = read();
//             a[i].push_back(make_pair(j, k));
//         }
//     }
//     int m = read();
//     rep(i, 1, m)
//     {
//         vector<int> v;
//         rep(j, 1, n)
//         {
//             int k = read();
//             v.push_back(k);
//         }
//         ++mp[v];
//     }
//     vector<int> v;
//     dfs(1, v);
//     for (auto x : output)
//         printf("%d ", x);
//     puts("");
//     return 0;
// }