1. 程式人生 > 其它 >Codeforces Round #620 (Div. 2) (A~D)

Codeforces Round #620 (Div. 2) (A~D)

比賽連結:Here

A題挺水的就不寫了

1304B - Longest Palindrome

題意:

  • 輸入 \(m\) 個長度為 \(n\) 的字串,問這些字串能組成的最長迴文串有多長。

思路:

  1. 貪心的思想,我們只需要用當前字串以及尋找該字串的反向串是否存在,如果存在的話,就把該字串與它的反向串新增進答案串的首尾。
  2. 注意中間的那個字串,如果我們輸入的字串中有迴文串,且該串的反向串也不存在的話,可以把該串單獨放在答案串的中間。
string s[110];
map<string, int>mp;
bool check(string s) {
    int n = s.size();
    for (int i = 0, j = n - 1; i <= n / 2; ++i, j--)
        if (s[i] != s[j]) return 0;
    return 1;
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m; cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> s[i], mp[s[i]] = 1;
    string ans, z, t;
    bool f = 0;
    for (int i = 0; i < n; ++i) {
        if (check(s[i])) z = s[i], f = 1;
        else {
            reverse(s[i].begin(), s[i].end());
            string tmp = s[i];
            reverse(s[i].begin(), s[i].end());
            if (mp[tmp] == 1) {
                t += tmp;
                mp[tmp] = 0;
            }
        }
        mp[s[i]] = 0;
    }
    ans += t;
    reverse(t.begin(), t.end());
    if (f) ans += z;
    ans += t;
    cout << int(ans.size()) << "\n";
    if (int(ans.size()))cout << ans << "\n";
}

1304C - Air Conditioner

題意:

  • 輸入 \(n\),再輸入 \(n\)​ 個顧客的資訊,分別為客人來的時間,客人需要的溫度範圍,每分鐘可以進行一次三種操作之一:升溫,降溫和不變。問能否滿足所有客人的需求。

思路:

一開始寫複雜了,這道題本質就是暴力模擬,要滿足所有客人的需求,那麼就用當前客人與下一個客人的時間差來取溫度的最大(r)和最小值(l),當下一個客人來時再判斷這個客人的需求是否在這個[l,r]之內。

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        ll n, m;
        cin >> n >> m;
        ll t = 0, l = m, r = m;
        bool f = 1;
        while (n--) {
            ll ti, x, y;
            cin >> ti >> x >> y;
            l = l - (ti - t), r = r + (ti - t);
            t = ti;
            l = max(l, x), r = min(r, y);
            if (l > r) f = 0;
        }
        cout << (f ? "YES\n" : "NO\n");
    }
}

1304D - Shortest and Longest LIS

題意:

  • 給定一個 \(n\) ,與構造規則,要求構造出符合構造規則的LIS最小和最大的串

思路:

原本想試試DP,寫狀態轉移發現最短序列的話,對於一串大於號,我們把當前未使用過的比較大的數儘可能的放在左邊。最長序列就是反過來,儘可能的放未使用過的小的數放在左邊即可

簡單來說就是貪心

const int N = 2e5 + 10;
ll a[N];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n; string s;
        cin >> n >> s;
        int nn = n, lst = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] == '>' || i == n - 1) {
                for (int j = i; j >= lst; j--)a[j] = nn--;
                lst = i + 1;
            }
        }
        for (int i = 0; i < n; ++i) cout << a[i] << " \n"[i == n - 1];
        nn = 1, lst = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] == '<' || i == n - 1) {
                for (int j = i; j >= lst; j--) a[j] = nn++;
                lst = i + 1;
            }
        }
        for (int i = 0; i < n; ++i) cout << a[i] << " \n"[i == n - 1];
    }
}

E,F不會做....明明挺多人都過了QAQ

The desire of his soul is the prophecy of his fate
你靈魂的慾望,是你命運的先知。