Educational Codeforces Round 93 (Rated for Div. 2)

Educational Codeforces Round 93 (Rated for Div. 2)

A. Bad Triangle

題意: 給定長度爲n的數組,給定n個數字,a[i] <= a[i + 1], 問是否可以找到ai,aj,ak,使得這三個數字組不成三角形。
題解: 一開始看錯題意,找三角形,寫了半個小時發現要找不能作三角形的。只要判斷前兩個數字和最後一個數字便可。由於前兩個數字最小,最後一個數字最大。
代碼:c++

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 10;
int a[N], n, T;

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        if (a[1] + a[2] <= a[n]) cout << 1 << " " << 2 << " " << n << endl;
        else cout << -1 << endl;
    }
    return 0;
}

B. Substring Removal Game

題意: 給你一個長度爲n的0,1字符串,兩我的輪流選擇其中很多於一位的連續子序列,得分爲所選子序列中1個數的總和,你先選,問最多可以得到多少分。
題解: 把連續的1分段拿出來,排序完倒敘,奇數和累加就是得分
代碼:數組

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 10;
int a[N], n, T;

int main() {
    cin >> T;
    while (T--) {
        string s;
        cin >> s;
        vector<int> cnt;
        int l = 0, r = 0;
        while (s[r] == '0' && r < s.size()) r++;
        // cout << r << endl;
        l = r;
        // cout << l << " " << r << endl; 
        while (1) {
            while (s[r] == '1' && r < s.size()) r++;
            // cout << r << endl;
            cnt.push_back(r - l);
            while (s[r] == '0' && r < s.size()) r++;
            l = r;
            if (r == s.size()) break;
        }
        
        int res = 0;
        sort(cnt.begin(), cnt.end());
        reverse(cnt.begin(), cnt.end());
        for (int i = 0; i < cnt.size(); ++i) {
            if (i % 2 == 0) {
                res += cnt[i];
            }
        }
        cout << res << endl;
    }
    return 0;
}

C. Good Subarrays

題意: 給你T組數據,每組給你一個長度爲n的字符串,求知足
\(\sum_{i=l}^{r}ai=r-l+1\) 的子區間的個數(i < j)
題解:\(\sum_{i=l}^{r}ai=r-l+1\)左右兩邊都減去r-l+1,則得\(\sum_{i=l}^{r}(ai-1)=0\),因此只須要找區間和爲0得個數。找區間和爲0得個數能夠使用前綴統計的方式,具體見代碼。
代碼:spa

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int const N = 1e5 + 10;
LL T, n, a[N], sum[N];
string s;
map<LL, LL> mp;

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        cin >> s;
        mp.clear();
        for (int i = 0; i < s.size(); ++i) a[i + 1] = s[i] - '0' - 1;
        for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
        // for (int i = 1; i <= n; ++i) cout << a[i] << endl;
        LL res = 0;
        // int l = 1;
        mp[0]++;
        // while (a[l] == 0 && l < n) res ++, l ++;
        for (int i = 1; i <= n; ++i) {
            // if (a[i] == 0) res++;
            res += mp[sum[i]];
            mp[sum[i]]++;
        }
        cout << res << endl;
    }
}

// 1 2 0
// 0 1 -1
// 1 + 
// 0 0 0

// 11011
// 0 0 -1 0 0
// 1 + 1 + 1 + 1 +

D. Colored Rectangles

題意: 有R,G,B三種顏色的棍子。如今告訴你每種棍子長度的對數和每對的長度,讓你用兩種不一樣顏色的棍子對圍成矩形,求每一個矩形加起來的最大值
題解: 把每一個棍子拿起來從大到小排序,而後dp暴力轉移
代碼:.net

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int const N = 210;
LL f[N][N][N], r, g, b, A[N], B[N], C[N];

int main() {
    cin >> r >> g >> b;
    memset(f, 0, sizeof f);
    for (int i = 1; i <= r; ++i) scanf("%lld", &A[i]);
    for (int i = 1; i <= g; ++i) scanf("%lld", &B[i]);
    for (int i = 1; i <= b; ++i) scanf("%lld", &C[i]);
    sort(A + 1, A + r + 1);
    reverse(A + 1, A + r + 1);
    sort(B + 1, B + g + 1);
    reverse(B + 1, B + g + 1);
    sort(C + 1, C + b + 1);
    reverse(C + 1, C + b + 1);
    for (int i = 0; i <= r; ++i) {
        for (int j = 0; j  <= g; ++j) {
            for (int k = 0; k <= b; ++k) {
                LL t1 = 0 , t2 = 0, t3 = 0;
                if (i && j)  t1 = f[i - 1][j - 1][k] + A[i] * B[j];
                if (i && k)  t2 = f[i - 1][j][k - 1] + A[i] * C[k];
                if (j && k)  t3 = f[i][j - 1][k - 1] + B[j] * C[k];
                
                f[i][j][k] = max(max(t1, f[i][j][k]), max(t2, t3));
            }
        }
    }
    LL res = 0;
    for (int i = 0; i <= r; ++i) 
        for (int j = 0; j  <= g; ++j) 
            for (int k = 0; k <= b; ++k) 
                res = max(res, f[i][j][k]);
    cout << res << endl;
    return 0;
}

參考

https://blog.csdn.net/xmyrzb/article/details/108020212code

相關文章
相關標籤/搜索